2

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

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

如果相关,我将 nginx 用于服务器。

4

1 回答 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",
    ];
于 2019-02-20T03:56:08.763 回答