The Tahaan
The Tahaan

Reputation: 7664

What HTTP status code should one use for invalid input

In my web service I am processing input as json. What is the correct HTTP status code to use when the provided input is invalid, particularly for PUT and POST requests?

The only response code I can find that seems to apply is "400 Bad Request", but maybe there is something better?

I actually have two possibly different specific cases of this question:

a) The server doesn't like the JSON input because the attributes/ values cannot be accepted, or
b) The server cannot parse the JSON formatted input because the data is not correctly formatted as JSON.

Example: If the server expects

{ 
  "POSTDATA": {
     "val1":"123", 
     "val2":"xyz"
   }
}

and the client gives something we don't know what to do with

{ 
    "val1":"bar", 
    "val2":"biz"
}

or the client gives something borked, eg

{
"valA":"123",
skjfhasklfhakppffffzzzzz....

What to do?

Upvotes: 3

Views: 5800

Answers (2)

Mike Fahy
Mike Fahy

Reputation: 5707

This is an ancient question now, but for what it's worth: Today (thanks to WebDAV http extensions), this error can best be served with a 422 (Unprocessable Entity) error code, as the format and syntax are correct, but the data itself cannot be used for its intended purpose.

Upvotes: 0

Amjad Alwareh
Amjad Alwareh

Reputation: 3291

415 Unsupported media type. The origin server is refusing to service the request because the payload is in a format not supported.

Upvotes: 1

Related Questions