Search code examples
resthttpresponsehttp-response-codes

What is the proper http response code for bad parameters


new user here.

I have a REST service which cannot work unless the parameters in the request are valid. I've looked over the list of possible HTTP response codes, and none of them seems quite right. 500 doesn't seem right, because it's actually an expected error. 404 isn't right because the ressource requested is available in principle. 406 is specific to content negotiation, which isn't what's happening here. And so on.

Is there a convention which HTTP response code to send to signify that the request is not valid for reasons of its content?


Solution

  • Is there a convention which HTTP response code to send to signify that the request is not valid for reasons of its content?

    422 Unprocessable Content

    The 422 (Unprocessable Content) status code indicates that the server
    understands the content type of the request content (hence a 415
    (Unsupported Media Type) status code is inappropriate), and the
    syntax of the request content is correct, but was unable to process
    the contained instructions.  For example, this status code can be
    sent if an XML request content contains well-formed (i.e.,
    syntactically correct), but semantically erroneous XML instructions.
    

    Is it OK to use that on a general HTTP request? I had considered it, but read somehwere it is specific to WebDAV.

    The original definition of 422 came from WebDAV (specifically RFC-4918), but the IANA status code registry currently uses HTTP Semantics as the standard reference.

    A general purpose HTTP component that doesn't recognize 422 should interpret the response as though it were a 400 Bad Request, which is fine.