How to disable chunked encoding for JSON responses in Laravel?

烈酒焚心 提交于 2019-12-11 00:05:59

问题


I'm returning an array from a controller method in Laravel. Laravel interprets this to mean I want to send JSON, which is great, but it doesn't set the Content-Length and instead uses Transfer-Encoding: chunked.

My responses are tiny, so I don't want to chunk them. How can I disable chunked encoding + enable Content-Length?

I'm using nginx for the server, if relevant.


回答1:


My solution is adding content-length headers to the response, then chunked-transfer will be replaced

    $responseJson = json_encode($response);

    $headers = [
        "Content-Length" => strlen($responseJson),
    ];

    return response($responseJson, 200, $headers);

you can try it with postman


for JSON content, just add content type in headers

    $headers = [
        "Content-Length" => strlen($responseJson),
        "Content-Type" => "application/json",
    ];


来源:https://stackoverflow.com/questions/41657052/how-to-disable-chunked-encoding-for-json-responses-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!