【问题标题】:How to disable chunked encoding for JSON responses in Laravel?如何在 Laravel 中禁用 JSON 响应的分块编码?
【发布时间】:2017-05-30 03:36:13
【问题描述】:

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

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

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

【问题讨论】:

  • 你找到解决办法了吗?
  • @roundtheworld 不。

标签: json http laravel-5.2 transfer-encoding


【解决方案1】:

我的解决方案是在响应中添加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",
    ];

【讨论】:

  • 那是设置正确的Content-Type (application/json) 吗?
  • 默认是text/html,可以自己设置
猜你喜欢
  • 2018-07-12
  • 1970-01-01
  • 2018-11-15
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 2012-08-20
  • 1970-01-01
相关资源
最近更新 更多