Search code examples
resthttp-error

What HTTP Error code to return if my Json is missing a field value?


For example, I have JSON like:

{
    "firstName":"",
    "lastName":"Gustafson",
    "dob":"1980-09-09T00:00:00.000+00:00"
}

If all 3 of these were required and given that above Json body is missing value for firstName, what HTTP error code would be considered a good fit to sent to clients from an API?

Would 400 Bad Request be considered a "bad request" even though it is well formatted but just missing a value for firstName?


Solution

  • If all 3 of these were required and given that above Json body is missing value for firstName, what HTTP error code would be considered a good fit to sent to clients from an API?

    I would normally use 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.

    403 Forbidden would also be a reasonable option.