Peter Lur
Peter Lur

Reputation: 758

Laravel remove header from JSON response

I am using the following code to return a Json encoded response from a function.

return response()->json($returnArray);

However the reponse is as follow and include the HTTP headers:

Cache-Control: no-cache, private
Content-Type:  application/json
Date:          Mon, 23 Oct 2017 15:34:59 GMT

{"status":"success"}  

How can I set the response so the headers are not included and only include the JSON?

{"status":"success"} 

Upvotes: 1

Views: 4225

Answers (2)

Ep1demic
Ep1demic

Reputation: 111

I had the some problem as you.

In my case problem was in string return type hinting in method.

Example:

public function getJson(): string{
    return response()->json(['foo' => 'bar']);
}

So, I replaced string to JsonResponse and that's all - problem solved.

Maybe would helps someone.

Upvotes: 1

Leo
Leo

Reputation: 7420

How about:

return response()->toJson([
    'status' => 'success',
], 201);

or:

return Response::json(['data' => $array],201);

Upvotes: 0

Related Questions