I'm trying to test my API using postman, but when I hit the POST
request, it's always throws me to the welcome page. I already set the XSRF token inside but still not working.
This is my api.php
routes:
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::resource('products/{product}/feedbacks', 'FeedbackController');
This is my store method from FeedbackController
:
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StoreFeedback $request
* @return \Illuminate\Http\Response
*/
public function store(Product $product, StoreFeedback $request)
{
$product->addFeedback(
$request->validated()
);
return response()->json([
"status" => "success",
"message" => "Your feedback has been submitted."
], 200);
}
Here is my web.php
file:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('product-categories', 'ProductCategoryController')->parameters([
'product_category' => 'productCategory'
]);
Route::resource('product-sub-categories', 'ProductSubCategoryController')->parameters([
'product_sub_category' => 'productCategory'
]);
And here is the screenshot of my postman request: Link to the screenshot
The problem lies in your App\Http\Requests\StoreFeedback
class.
Cause you're passing the request through the Form Validator. Which invalidates the form request. And that's why passing the request back to the previous URL which becomes the /
by default.
Invalidates the form
Finds the previous URL
Url Resolver
But, if you want to get the errors, you can simply pass the HEADER Accept:application/json
to request HEADER and you'll get the errors.
Reason: ValidationException handled here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With