Laravel - Return json along with http status code

laravel return json response with status code
http response codes
laravel return error response
laravel return only status code
how to set header in laravel api
json in laravel
laravel return json response from controller
laravel api error handling

If I return an object:

return Response::json([
    'hello' => $value
]);

the status code will be 200. How can I change it to 201, with a message and send it with the json object?.

I don't know if there is a way to just set the status code in Laravel.


You can use http_response_code() to set HTTP response code.

If you pass no parameters then http_response_code will get the current status code. If you pass a parameter it will set the response code.

http_response_code(201); // Set response status code to 201

For Laravel(Reference from: https://stackoverflow.com/a/14717895/2025923):

return Response::json([
    'hello' => $value
], 201); // Status code here

php - Laravel - Return json along with http status code, If I return an object: return Response::json([ 'hello' => $value ]);. the status code will be 200. How can I change it to 201, with a message and send it with the json​  where STATUS_CODE is your HTTP status code you want to send. Both are identical. if you are using Eloquent model, then simple return will also be auto converted in JSON by default like,


This is how I do it in Laravel 5

return Response::json(['hello' => $value],201);

Or using a helper function:

return response()->json(['hello' => $value], 201); 

HTTP Responses - Laravel, If I return an object: return Response::json([ 'hello' => $value ]); the status code will be 200. How can I change it to 201, with a message and send it with the json​  Of course, if you do not need to pass a custom HTTP status code or custom headers, you should use the global view helper function. JSON Responses The json method will automatically set the Content-Type header to application/json , as well as convert the given array to JSON using the json_encode PHP function:


I think it is better practice to keep your response under single control and for this reason I found out the most official solution.

response()->json([...])
    ->setStatusCode(Response::HTTP_OK, Response::$statusTexts[Response::HTTP_OK]);

add this after namespace declaration:

use Illuminate\Http\Response;

Laravel - Return json along with http status code, Laravel - Return json along with http status code. If I return an object: return Response::json([ 'hello' => $value ]);. the status code will be 200. How can I change  The Http facade's fake method allows you to instruct the HTTP client to return stubbed / dummy responses when requests are made. Faking Responses. For example, to instruct the HTTP client to return empty, 200 status code responses for every request, you may call the fake method with no arguments:


There are multiple ways

return \Response::json(['hello' => $value], STATUS_CODE);

return response()->json(['hello' => $value], STATUS_CODE);

where STATUS_CODE is your HTTP status code you want to send. Both are identical.

if you are using Eloquent model, then simple return will also be auto converted in JSON by default like,

return User::all();

Laravel - Return json along with http status code, Get code examples like "php laravel return json response" instantly right from your google search results with the Grepper Chrome Extension. In this article, we will discuss Laravel 5.6 JWT Auth. Firstly, you need to know what is JWT. JWT (JSON Web Tokens), it allows us to represent user data in a secure manner. JWT Auth is used when we work with API. User sign-in using its own email and password.


return response(['title' => trans('web.errors.duplicate_title')], 422); //Unprocessable Entity

Hope my answer was helpful.

php laravel return json response Code Example, GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. Sign up. Several of the responses rely on an exception being thrown and having it handled in the OnException override. In my case, I wanted to return statuses such as bad request if the user, say, had passed in a bad ID.


http response status code always return 200 · Issue #1007 · laravel , He was trying to make requests to the Laravel backend and told me he kept receiving a response with a http status code of 302. 3XX http status  If the request was an AJAX request, a HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors. In other words - there is no reason you cannot do this in a simple FormRequest and simply your code significantly.


Force Laravel to return JSON responses, @2serve use: //example return response()->setStatusCode(201, 'The resource is created successfully!');. use the helper function in laravel 5.1 instead:. return response()->json(['name' => 'Abigail', 'state' => 'CA']); This will create an instance of \Illuminate\Routing


Laravel 5.1 Custom HTTP Status Code and Status Text, Retrieving IP address · Retrieving Current Route · Using Eloquent with Slim Your Slim app's routes and middleware are given a PSR-7 response object that The status code identifies the type of HTTP response to be returned to the client. you wish the client to be redirected to along with an optional $status code. Today I was working on an API written in Laravel for a React Native app with another developer. He was trying to make requests to the Laravel backend and told me he kept receiving a response with a http status code of 302. 3XX http status codes are redirection status codes.