简体   繁体   English

http状态代码102缺少响应标头

[英]Response headers missing with http status code 102

I'm currently developing a REST API in which I need to return a 102 HTTP status code (processing) while I'm generating an export. 我正在开发一个REST API ,我需要在生成导出时返回102 HTTP status code (处理)。

Workflow : 工作流程:

  1. POST /exports POST / exports
    • return 201 with data 用数据返回201
  2. GET /exports/id GET / exports / id
    • return 102 with data if the export is processing 如果导出正在处理,则返回带有数据的102
    • return 200 with data if the export is completed 如果导出完成,则返回带有数据的200

When I try to retrieve export data while it's processing, there are no response headers: response headers are missing with 102 HTTP status code. 当我尝试在处理时检索导出数据时,没有响应头:102 HTTP状态代码缺少响应头。 If I change the status code with 2xx for instance, it's working fine. 例如,如果我用2xx更改状态代码,它工作正常。 I can't figure out. 我弄不清楚。 Is there anything specific with the 102 HTTP status code? 102 HTTP状态代码有什么特定的吗? When I say response headers are missing I mean: Chrome > Developer tools > Network Tab > Click on request > Headers tab > Only showing "General" and "Request Headers" (same with FF & Postman) . 当我说响应标题丢失时,我的意思是: Chrome > Developer tools > Network Tab > Click on request > Headers tab > Only showing "General" and "Request Headers" (same with FF & Postman)

Used Technologies : 二手技术:

  • Ubuntu 18.04 LTS Ubuntu 18.04 LTS
  • PHP 7.2 (latest release) PHP 7.2(最新版本)
  • laravel/lumen 5.6.21 laravel / lumen 5.6.21
  • Apache 2.4.29 Apache 2.4.29

Controller Code : 控制器代码:

 /**
 * Return export by id
 *
 * @param int $id
 * @return \Illuminate\Http\JsonResponse
 *
 * @throws AuthorizationException
 * @throws ModelNotFoundException
 */
public function getItem(int $id)
{
    if($export = Export::find($id))
    {
        $this->authorize(__FUNCTION__, $export);

        if($export->status != Export::STATUS_COMPLETED)
        {
            return response()->json($export, 102);
        }

        return response()->json($export);
    }

    throw new ModelNotFoundException();
}

Expected Request Headers : 预期请求标题:

  • Access-Control-Allow-Origin 访问控制允许来源
  • Cache-Control 缓存控制
  • Connection 连接
  • Content-Length 内容长度
  • Content-Type 内容类型
  • Date 日期
  • Proxy-Connection 代理连接
  • Server 服务器
  • Vary 变化

EDIT 编辑

I should have mentioned that it worked on my previous config : 我应该提到它在我以前的配置上工作:

  • Ubuntu 17.10 LTS Ubuntu 17.10 LTS
  • PHP 7.1 (latest release) PHP 7.1(最新版本)
  • laravel/lumen 5.6.16 laravel / lumen 5.6.16
  • Apache 2.4.27 Apache 2.4.27

I haven't found in any release notes what could have impacted the request answer. 我没有在任何发行说明中找到可能影响请求答案的内容。

Use HTTP 202 Accepted instead. 请改用HTTP 202 Accepted

See: https://softwareengineering.stackexchange.com/questions/316208/http-status-code-for-still-processing 请参阅: https//softwareengineering.stackexchange.com/questions/316208/http-status-code-for-still-processing

Explained: 解释:

RFC 2518 says "The server MUST send a final response after the request has been completed", and this is interpreted to mean that your server needs to send an final response code in addition to the initial HTTP 102 . RFC 2518说“服务器必须在请求完成后发送最终响应”,这被解释为意味着您的服务器除了初始HTTP 102 之外还需要发送最终响应代码。 Not doing so creates timeout problems for clients waiting for a final response but not getting one. 不这样做会为等待最终响应但没有获得最终响应的客户端创建超时问题。 Firefox will choke, Chrome will time out and convert it into HTTP 200 OK . Firefox会窒息,Chrome会超时并将其转换为HTTP 200 OK cURL will inform that there is unread content. cURL将通知未读内容。

So use HTTP 102 Processing only as a hint to clients that, "Okay, but this might take a minute...", after which you follow up with a final code and response body. 因此,仅使用HTTP 102 Processing作为对客户的提示,“好的,但这可能需要一分钟......”,之后您将跟进最终的代码和响应正文。

If it is a long running process that you want to periodically poll, use HTTP 202 Accepted and close the response. 如果您要定期轮询一个长时间运行的进程,请使用HTTP 202 Accepted并关闭响应。

It's also worth noting that http_response_code() does not handle HTTP 102 . 值得注意的是, http_response_code()不处理HTTP 102

Bad Example: 不好的例子:

<?php header('HTTP/1.1 102 Processing'); exit; ?>

Good Example: 好例子:

<?php
header('HTTP/1.1 102 Processing'); // let client know it might take a while
sleep(2); // do stuff that takes a while
header('HTTP/1.1 200 OK'); // counterintuitive, but works

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM