0

I would like to know if the HTTP error code 412 is used correctly on our RESTful api.

We have one URL which regenerates some data on request, and it has some logic in the server-side to check internally if there is nothing new to regenerate it will retrieve 412 status code. If there are some changes on server it will regenerate data and it will retrieve 200 status code.

I am thinking that 412 is not a good choice for error, because the condition checking is happening in server-side internally. So I think that using status code 200 for both cases would be probably a better approach. Finally the URL would respond with JSON which would contain data about the generation status.

sarunast
  • 2,443
  • 3
  • 27
  • 37

1 Answers1

1

Precondition failed is definitely misused in the described scenario. It should be used in strict correlation with if-family headers, such as: If-Match or If-None-Match, etc.

Codes that are useable in this case are: 200, 204 and 409.

  1. 200 - in both cases (data generated, no generation) and with a meaningful body - data or message that there was no need for generation

  2. 200 - when data were generated and 204 when no generation took place.

  3. 200 - when data were generated and 409 when no generation took place.

In every case (except 409) you should include a meaningful message or the data itself.

404 won't be useful here - this is not the case that the endpoint wasn't found.

Opal
  • 81,889
  • 28
  • 189
  • 210