HTTP 404 vs 400 for invalid query parameters

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



HTTP 404 vs 400 for invalid query parameters



Here is my request URL:



http://server.com/app/user/getuser/?userId=9999



Note that userId is query parameter. Not embedded path parameter.


userId



I understand that if the request URL is: http://server.com/app/user/getuser/9999 and ID 9999 does not exist in database, 404 should be used.



BUT what HTTP status should be used for the case userId is query parameter? Right now I am returning 400 instead of 404.


userId




1 Answer
1



I would use 404 Not Found.



The RFC 7231 defines a 400 Bad Request response like this:



The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).



...since your request is valid and you are just trying to access a resource that does not exist, I think a 404 Not Found status is more adapted. RFC 7231 defines its meaning like this:



The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists.





Upvoted. Thanks!
– Loc
Aug 6 at 15:52






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard