Hi! I'm building a RESTful API with Laravel 5.4. All's fine with my tests, my API returns the good HTTP response code (200, 422, 404, ...) but when I'm trying to use it in a real case, the HTTP code 200 is always returned.
return response()->json([
'name' => ['The field name is required']]
], 422);
// Works
$this->postJson(route('api.users.create'))
->assertStatus(422)
curl --request POST \
--url http://my-endpoint.rest \
--header 'accept: application/json' \
--header 'content-type: multipart/form-data' \
It's cURL but I have the same problem with Axios and Insomnia
My request always returns 200. And it's the same for all of my controllers.
I've tried with Response::make()
and abort()
this is the same thing. Routes are in the "api" group and the Content-Type
returned is always text/html
.
+1
I can't replicate your issue, response codes are returned correctly.
Route::get('/user', function (Request $request) {
return response()->json(123, 422);
});
Returns a response code 422.
Found the error in routes/api.php
.
The empty line converts all request to HTTP 200
.
Can confirm, empty line before opening <?php
leads to wrong (always 200) status code and content type
Happened here too. VS Code warns if you happen to have a namespace statement in the file.
Without the namespace you get no warnings.
Hi, I have a similar problem, but with routes/api.php all okay. What else could be the problem?
@yourlenar I think it's not only the routing files, can you check all the other project files too?
Most helpful comment
Found the error in
routes/api.php
.The empty line converts all request to
HTTP 200
.