Reputation: 2248
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
Upvotes: 6
Views: 6310
Reputation: 828
My solution, add failedValidation function in Request
public function failedValidation(Validator $validator) {
throw new HttpResponseException(response()->json([
'success' => false,
'message' => 'Validation errors',
'data' => $validator->errors()
]));
}
Upvotes: 0
Reputation: 1
I ran into the same issue but it was my fault. I didn't add the right headers.
Make sure you specify "Content-Type" and "Accept". Both should be set to "application/json". See image below.
Hope this helps.
Upvotes: 0
Reputation: 571
As mentioned above this is happening because of the form validator, You can also fix this using middlewares, first create a middleware:
php artisan make:middleware JsonRequestMiddleware
Update the handle method of your middleware
public function handle(Request $request, Closure $next)
{
$request->headers->set("Accept", "application/json");
return $next($request);
}
then add this middleware to app/Http/Kernel
protected $middleware = [
...
\App\Http\Middleware\JsonRequestMiddleware::class,
...
]
Upvotes: 1
Reputation: 3694
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.
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
Upvotes: 23
Reputation: 2248
I'd just found the problem, it was from the app\Http\Requests\StoreFeedback
like Mr.ssi-anik said. I don't know why for the boolean validation, when I put true
or false
, it fails and redirect me to the welcome page.
Instead, I used 0
or 1
, it accepts the parameters and working normally.
Upvotes: 1
Reputation: 2636
I think It's showing the same page because you're using the Route::resource
, as said in this Question,
A RESTful resource controller sets up some default routes for you and even names them.
Route::resource('users', 'UsersController');
Gives you these named routes:
Verb Path Action Route Name
GET /users index users.index
GET /users/create create users.create
POST /users store users.store
GET /users/{user} show users.show
GET /users/{user}/edit edit users.edit
PUT|PATCH /users/{user} update users.update
DELETE /users/{user} destroy users.destroy
My guess is: you're basically calling the index()
function in your FeedbackController over and over again
Change your route to:
Route::post('products/{product}/feedbacks', 'FeedbackController@store');
EDIT change your controller function to:
public function store(Request $request)
{
dd($request->body); // or the key you send it on the postman
}
And show us what you got
Upvotes: 1