【问题标题】:Why Lumen send the response with headers into the body?为什么 Lumen 将带有标题的响应发送到正文中?
【发布时间】:2019-09-21 03:34:31
【问题描述】:

当我调用 Lumen 时,无论使用何种方法,它总是返回带有响应的标头。为什么?

这是路由器:

$router->group(['prefix' => 'test'], function() use ($router) {

    $router->post('an', 'MyController@anAction');
});

这是来自我的控制器的操作

public function anAction(Request $request): string {

    $return['result'] = true;
    return response()->json($return);
}

这是回应:

HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Type: application/json Date: Thu, 02 May 2019 14:54:35 GMT {"result":true}

我怎么能有这个?

{"result":true}

【问题讨论】:

  • 你的中间件是什么样的?
  • 我没有使用任何中间件,安装时这个文件夹里只有authenticate.php。
  • 也可能是您的 Web 服务器发送标头信息,例如阿帕奇
  • 这将是令人惊讶的。对于另一个项目,我在同一环境中运行的 restfull API 中使用 Slim (symfony),但我没有这种行为。
  • 另外,当我使用return json_encode($return); 而不是return response()->json($return); 时它工作正常。那么Response 对象有什么问题呢?

标签: http-headers httpresponse lumen


【解决方案1】:

我刚遇到这个问题,是由函数的typehint返回引起的:

public function anAction(Request $request): string {

这是将响应转换为字符串,而不是实际的 Illuminate\Http\JsonResponse。将其更改为此解决了我的问题。

public function anAction(Request $request): \Illuminate\Http\JsonResponse {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    相关资源
    最近更新 更多