如何在Laravel中禁用JSON响应的分块编码?

时间:2017-01-15 01:57:50

标签: json http laravel-5.2 transfer-encoding

我从Laravel中的控制器方法返回一个数组。 Laravel解释这意味着我想发送JSON,这很好,但它没有设置Content-Length而是使用Transfer-Encoding: chunked

我的回答很小,所以我不想把它们分块。如何禁用分块编码+启用Content-Length?

如果相关,我正在为服务器使用nginx。

1 个答案:

答案 0 :(得分:0)

我的解决方案是在响应中添加content-length标头,然后替换chunked-transfer

    $responseJson = json_encode($response);

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

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

您可以和邮递员一起尝试


对于JSON内容,只需在标题中添加内容类型

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