All API request must specify Accept: header to tell laravel that a json reply is expected.
Laravel API returning webpages
First time here! I've been testing converting my first project into an API and everything is going well so far except for one thing: Laravel for some reason returns webpages when it should exclusively return JSON along with a status code no matter the circumstances.
I'm using Postman to test my routes and I discovered that they return a webpage when:
- I try to send a request that doesn't have the header 'Accept' specifically set to 'application/json'; and
- I purposefully send a bad request (i.e. the password confirmation is wrong, or some keys were not sent) to trigger the request validation logic.
When I looked at the data received by Postman, it has a status of 200 with the body being the HTML to the home page when it should've been 422 with a body of the JSON of the request validator.
Looking at the logs of the server, it appears that immediately after accessing the requested route ('/api/register'), it immediately requests the index page ('/').
I have some guesses on why this happens but I'm not sure. I've tried the workaround of forcing JSON response using a middleware but it doesn't seem to work. What is happening here and how do I fix it?
Update: For future reference, my complete solution is:
- I made a middleware named
ForceJsonMiddleware:
class ForceJsonMiddleware
{
public function handle(Request $request, Closure $next): Response
{
if(!$request->wantsJson()) {
abort(response()->json([
'message' => 'This resource only supports JSON responses (set header "Accept" to "application/json").'
], 400));
}
return $next($request);
}
}
- For convinience, middleware alias is set in
bootstrap/app.php:
$middleware->alias([
'force-json' => ForceJsonMiddleware::class,
]);
- To prevent conflicts with middleware priority (especially when using
authmiddleware), the middleware priority is set to:
$middleware->priority([
ForceJsonMiddleware::class
]);
Please or to participate in this conversation.