Amit Shah
Amit Shah

Reputation: 8179

Laravel: custom http status code not working

Laravel version: 5.5

I am trying to return custom http status code from the laravel controller. (Calling this url using jQuery Ajax $.get())

In my controller function I tried both the way mentioned bellow but it's not working.

  1. This one returns error "Method setStatusCode does not exist."

    return response()->setStatusCode(202);
    
  2. This one not throwing error but returning 200 always.

    $response = new Response();
    $response->setStatusCode(202);
    $response->header('custom', 555);
    return $response;`
    

Upvotes: 2

Views: 7807

Answers (3)

Murat Akbulut
Murat Akbulut

Reputation: 53

Let me present a few more methods of use. Maybe you are looking for something like this without json.

response(null)->setStatusCode(202);
response(null, 202);

Upvotes: 0

simonecosci
simonecosci

Reputation: 1204

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

Upvotes: 1

mutas
mutas

Reputation: 359

Use it like this:

return response()->json("response content", 202);
//or
return response()->make("response content", 202);

Check https://laravel.com/api/5.0/Illuminate/Routing/ResponseFactory.html for more detailed documentation.

Upvotes: 5

Related Questions