Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

caleuanhopkins's avatar

Lumen JSON limit

I have a large piece of text in my DB stored and can successfully pull it from my DB, dd() it and log::debug() the content with no issues. The text can be seen here: https://pastebin.com/KQNYW623

The problem comes when I try to return a JSON response of the text. On a clean Lumen 5.7 install in the api.php route file I have this route:

$router->get('/', function () use ($router) {
    return json_encode("*insert large content here*"); //this is where the big text goes, I won't paste it fully here but it's here in my code as a string
});

If I access this route, I get a blank page. Absolutely nothing appears in either my screen, Postman or curl. There's no errors in the error logs, nothing. Just blank.

If I place anything else that is much smaller than the large text (like hello world) I have no problem outputting the response. Can someone please shed some light on what the root cause of this issue is? Is there a character limit for responses in Lumen/Laravel?

0 likes
8 replies
D9705996's avatar

What logs did you look in? Have you checked your webserver logs and php_errors? I suspect t the issue is related to something like the php memory limit being exceeded.

If you try changing you route to

$router->get('/', function () use ($router) {
    ini_set('memory_limit','-1'); // turn off memory limit
    return json_encode("*insert large content here*"); 
}

If this works then you need to change your php.ini setting for memory limit to something more appropriate. Depending on the size of you data you may also need to tweak some webserver settings

caleuanhopkins's avatar

Nothing in logs, no errors are thrown. Have done checks for JSON error the json_encode() actually works fine. If I don't do return response() and just do a print_r() I can see the data fine. The problem lies with the response() method from either Laravel's Response library of Symphony's. I have tested the response() with a large plaintext string as well and I still get blank responses.

Have tested your memory suggestion but didn't work or make any difference. Outside of Lumen (i.e at the top of the public/index.php file), I have done echo json_encode() and tried the string there and the output was successful. This is what leads me to believe there is an issue with the string length with Laravel or Symphony are building the HTTP response. No idea why though :/

D9705996's avatar

I just tested with the content from your pastebin on my local setup

return response(...);  // Your string data in the function argument

Worked without any errors and I get the following in my browser

<p>HTML-Ipsum Info </p> <p>You’ve probably heard of Lorem Ipsum before ...

Therefore it looks like something environmental to your setup. Try turning on debug logging in your webserver/php/etc and see if you can find something to go on to help isolate the source of the fault.

caleuanhopkins's avatar

If I can output the string in the public/index.php file using echo json_encode() why should it be anything to do with the environment setup? Unless Symphony or Laravel are pulling in some weird setting to produce the header, the echo test shows that the server it fine to output the large string in the browser

D9705996's avatar

@CALEUANHOPKINS - The reason I suggested something environmental was because I could not replicate your fault when I used your code and data in my setup.

caleuanhopkins's avatar

Yeah I appreciate that, however if echo works on my server, I can't see how Laravel's return response()'s issue is to do with the PHP environment unless Laravel pulls in an environment variable that raw PHP doesn't.

That aside, I have discovered that if I do echo response()->json() the string is actually parsed into JSON successfully and the headers are attached to the response. I'm now wondering why then the return is placed in front, why it comes back blank. The headers added are: HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Type: application/json Date: Fri, 30 Nov 2018 09:38:47 GMT. Can't see any reason why this response would come back blank on a return.

caleuanhopkins's avatar
caleuanhopkins
OP
Best Answer
Level 1

Found the issue: I have a CORS middleware running that was causing an issue to response with a high content length. Not 100% sure why it was only being triggered on content length of 6K+ but I've removed it now and the response is working fine now.

Thanks for the hints and advice @d9705996

Please or to participate in this conversation.