Can't you just use this? It will auto set the content type to application/json
return response()->json($cached_data);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am storing data as json in Redis. When I get a cache hit, I am retrieving this json-formatted data from Redis and then decoding it before returning.
That seems like a silly step since it just get re-encoded before sending the response. So I simply returned the json straight from Redis. It saves a good 20 to 30 ms. However it is sending the data as text/html and not application/json.
When I tried setting the content type as so:
return response($cached_data)
->header('Content-Type', 'application/json');
... it simply sends the header as a part of the text/html and not a header.
Any idea how to properly set the content type from the api route file?
Please or to participate in this conversation.