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.
Workflow :
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. If I change the status code with 2xx for instance, it's working fine. I can't figure out. Is there anything specific with the 102 HTTP status code? 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)
.
Used Technologies :
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 :
I should have mentioned that it worked on my previous config :
I haven't found in any release notes what could have impacted the request answer.
Use HTTP 202 Accepted
instead.
See: 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
. 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
. cURL will inform that there is unread content.
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.
If it is a long running process that you want to periodically poll, use HTTP 202 Accepted
and close the response.
It's also worth noting that http_response_code()
does not handle 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.