What is the correct http status code when URI parameter is wrong
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 lik开发者_JAVA技巧e /search?term=whatisearch
When the client does not send the expected data, what would be a correct error code?
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.
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.
精彩评论