开发者

What is the correct http status code when URI parameter is wrong

开发者 https://www.devze.com 2023-02-26 03:26 出处:网络
Given i have a HTTP-Interface, and for example POST /user expects some specific JSON to be posted (for example {\"username\": \"keppla\"}), or

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号