2

I am writing a REST service and some of the push requests will only work during a certain window of time. For example during active work hours. Outside of those times the server will send an error.

I have looked at the available HTTP status codes and I am not sure which one best to apply for an 'invalid server state' or equivalent situation. I am considering a 400 (Bad Request) or a 422 (Unprocessable Entity)?

For the 422, the definition I have is "The request was well-formed but was unable to be followed due to semantic errors." and wondering whether this is really the most applicable case?

Andre M
  • 6,649
  • 7
  • 52
  • 93
  • If it's a server side problem, why don't you use one of the 5xx Server Error codes? – Dave V Aug 24 '16 at 17:15
  • Generally I see 5xx codes when something is broken. Nothing is technically broken, rather the client is asking to do something that can't be fulfilled, by parameters that can be validated, prior to the operation. The client can ask what the acceptable work hours are, in a separate request. – Andre M Aug 24 '16 at 17:35

1 Answers1

3

400 Bad Request looks like the right response to me. It's definitely a client error, but there's nothing wrong with the request itself; just the timing of the request. If the response body contains some additional information to make that clear (along the lines of "Our offices are closed. Please make your request between the hours of 9 AM and 5 PM GMT, Monday to Friday.") then you've successfully used a simple and common response type in the appropriate manner. Which makes for a good API.

As an additional note; the reason I'd say that a 422 would be less correct is that the meaning of the request is clear. It's just a timing issue, there's no semantic error.

Task
  • 3,668
  • 1
  • 21
  • 32