Search code examples
httphttp-status-codeshttp-status

Which HTTP Status code should indicate failed to carry out some code due to invalid input?


I have a POST request that require a user id. If that user id is found, then everything is successfully is carried out and the default http code is returned (i.e. 200). If the user id is not found, what should I return? Non of the 4xx seem to fulfill my case.


Solution

  • I have a POST request that require a user id.

    But how is this id sent to the server?

    Your question lacks plenty of details on what you are trying to achieve, so it's tricky to suggest something. But here are some scenarios I can think of:

    • If a representation for the resource with the given identifier sent in the URI cannot be found, then 404 would be the proper choice.

    • If the value of a query parameter is invalid (like if the server expects an integer but an alpha character is given) and the request cannot be processed because of it, then 400 would be suitable.

    • If an invalid value has been sent as part of the body of the request (like if a validation fails or the expected value is missing) and the request could not be processed because of it, then you should consider 422.