John Dorean
John Dorean

Reputation: 3874

Forcing Laravel response status code

I'm building a REST API in Laravel and when a new user is created I want to return a 201 status code and a Location header to point to the new resource.

Most of that is being achieved using this code:

$response = Response::make(null, 201)->header('Location', Config::get('app.url') . '/v1/users/' . $user->id);
return $response;

However Laravel seems to be overriding what I set as the status code because I'm setting a Location header, as I'm getting a 302 Moved Temporarily header back.

How can I force a 201 status code even when I'm specifying a Location header?

Upvotes: 1

Views: 1862

Answers (1)

Laurence
Laurence

Reputation: 60030

The problem is when you set the header() location, it is overwriting the Laravel response.

If you add 201 status to the header() function - it should work I think:

$response = Response::make(null, 201)->header('Location', Config::get('app.url') . '/v1/users/' . $user->id, true, 201);
return $response;

Upvotes: 2

Related Questions