Framework: [5.4] Response - Wrong HTTP status code returned

Created on 3 Jul 2017  路  8Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.24
  • PHP Version: 7.0.15
  • Database Driver & Version: MySQL 5.6

Description:

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.

Steps To Reproduce:

  1. My controller
return response()->json([
   'name' => ['The field name is required']]
], 422);
  1. My test
// Works
$this->postJson(route('api.users.create'))
        ->assertStatus(422)
  1. My real request
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.

Most helpful comment

Found the error in routes/api.php.

capture d ecran 2017-07-03 a 13 45 58

The empty line converts all request to HTTP 200.

All 8 comments

+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.

capture d ecran 2017-07-03 a 13 45 58

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.

screen shot 2018-04-13 at 3 31 54 pm

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GrahamCampbell picture GrahamCampbell  路  139Comments

JosephSilber picture JosephSilber  路  176Comments

MountainDev picture MountainDev  路  128Comments

thewinterwind picture thewinterwind  路  63Comments

nkeena picture nkeena  路  75Comments