Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP statuscode to retry same request

Is there an HTTP status code to instruct a client to perform the same request again?

I am facing a situation where the server has to "wait" for a lock to disappear when processing a request. But by the time the lock disappears, the requests might be close to its timeout limit. So instead, once the lock clears, I would like to instruct the client to just perform the same request again.

The best I an come up with is a HTTP 307 to the same location, but I'm worried that some browsers might not buy into this (redirect loop detection).

like image 623
Jeroen Ooms Avatar asked Jul 25 '13 15:07

Jeroen Ooms


People also ask

Which HTTP status codes can be retried?

HTTP status codes and the error message can give you a clue. In general, a 5xx status code can be retried, a 4xx status code should be checked first, and a 3xx or 2xx code does not need retried.

How do you use retry-after?

The Retry-After response HTTP header indicates how long the user agent should wait before making a follow-up request. There are three main cases this header is used: When sent with a 503 (Service Unavailable) response, this indicates how long the service is expected to be unavailable.

What is the HTTP status code for duplicate record?

409 Conflict - Client attempting to create a duplicate record, which is not allowed. 410 Gone - The requested resource has been deleted. 411 Length Required - The server will not accept the request without the Content-Length Header.

What Statuscode 303?

The HyperText Transfer Protocol (HTTP) 303 See Other redirect status response code indicates that the redirects don't link to the requested resource itself, but to another page (such as a confirmation page, a representation of a real-world object — see HTTP range-14 — or an upload-progress page).


1 Answers

The correct response, when a server is unable to handle a request, is 503 Service Unavailable. When the condition is temporary, as it is in your case, you can set the Retry-After header to let the client know how long it should wait before trying again.

However, this will not force the browser to perform the request again - this is something you would need to handle yourself in javascript. For example, here is how you might perform a retrying ajax POST request in jquery:

function postData() {   $.ajax({     type: 'POST',     url:  '503.php',     success: function() {       /*          Do whatever you need to do here when successful.       */     },     statusCode: {       503: function(jqXHR) {         var retryAfter = jqXHR.getResponseHeader('Retry-After');         retryAfter = parseInt(retryAfter, 10);         if (!retryAfter) retryAfter = 5;         setTimeout(postData, retryAfter * 1000);       }     }   }); } 

Note that the above code only supports a Retry-After header where the retry delay is specified in seconds. If you want to support dates that would require a little more work. In production code I would also recommend a counter of some sort to make sure you don't keep retrying forever.

As for using a 307 status code to repeat the request automatically, I don't think that is a good idea. Even if you add a retry parameter to get around the browser loop detection (which feels like a horrible hack), it's still not going to work on a POST request. From RFC2616:

If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user.

While some browsers are known to ignore this requirement, it's definitely not correct, and isn't something you would want to rely on.

And if you're not using a POST request, you almost certainly should be. Remember that a GET request should not have any side effects, and by default the response will be cached. From the description of your problem, it sounds very much like your request is likely to be doing something that has side-effects.

like image 139
James Holderness Avatar answered Nov 09 '22 19:11

James Holderness