0

Given i have a HTTP-Interface, and for example

POST /user

expects some specific JSON to be posted (for example {"username": "keppla"}), or

GET /search

expects a parameter like /search?term=whatisearch

When the client does not send the expected data, what would be a correct error code?

8
  • en.wikipedia.org/wiki/… Commented Apr 18, 2011 at 9:28
  • i read this list, but none of them strikes me as obvious. Commented Apr 18, 2011 at 9:32
  • just send a 200 with a json error object, it is more customisable in most cases that way anyway Commented Apr 18, 2011 at 9:43
  • 2
    @davin That would be a bad idea. Commented Apr 18, 2011 at 11:13
  • 1
    hmmm, i could have sworn i wrote 400, and its too late to edit... Commented Apr 18, 2011 at 12:23

2 Answers 2

6

400-499 is range for incomplete client requests. Looks like 400 is the right error code for your use case.

400 - Bad Request, The server could not understand the request, probably due to a syntax error.

Sign up to request clarification or add additional context in comments.

5 Comments

i felt, the 'probably due to syntax error' would be kind of misleading (the requests are syntactically correct).
There is also 417, Expectation failed, Value in the Expect request header could not be met
but there's no request expectaion. As i understand 417, it is to answer to requests with 'expect' headers. 400 seems to be more apropriate
hmm yeah right, 417 is about the server not able to meet the expectation w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18 400 seems appropriate.
400 seems appropriate. Syntax error as in your application is expecting /search?term=whatever but unexpectadly request ended with /search or requested contains unexpected /search?term=' inject sql'
4

422 is a possibility, but it is not commonly used, as it is not widely known that it is permissible to use WEBDAV status codes even when you are not doing WEBDAV.

422 - The request was well-formed but was unable to be followed due to semantic errors

For the second scenario, you could argue that a 404 is more appropriate as it is the URI that is not correctly formed, rather than the request body.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.