Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.8 POST request always throws welcome page

Tags:

php

laravel

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

like image 772
Tamma Avatar asked Mar 03 '19 00:03

Tamma


1 Answers

POST request always throws the welcome page

The problem lies in your App\Http\Requests\StoreFeedback class.

How?

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.

Hierarchy below

  • 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

like image 138
ssi-anik Avatar answered Sep 28 '22 08:09

ssi-anik