Home > OS >  What should be the http error code for passing blank value in a mandatory parameter which should not
What should be the http error code for passing blank value in a mandatory parameter which should not

Time:10-15

for example:

post method:

{
  name = "",
  id = 1,
  class = 4
}

name should not be a blank parameter but i am still sending it this way in a post/put request call. what should be the output error code for such data

CodePudding user response:

Many use 400 Bad Request. This used to indicate bad syntax, but the latest HTTP RFC makes it more expansive.

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).

I prefer the more specific 422 Unprocessable Entity. This indicates that the syntax was fine, but the semantics were wrong.

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

Technically 422 is from WebDAV, not HTTP, but it doesn't matter. Many APIs use 422.

Either is acceptable.

See also What HTTP status response code should I use if the request is missing a required parameter?.

  • Related