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

问题描述 投票:2回答:1

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

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

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

json http laravel-5.2 transfer-encoding
1个回答
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",
    ];
© www.soinside.com 2019 - 2024. All rights reserved.