Question

We are building public RESTful JSON API and trying to figure out what is the best practice for POST/PUT requests when specified property doesn't exist. Should we throw an HTTP error and which one or just skip it and apply all other properties?

{
    "name" : "Test",
    "property_doesnt_exist" : "that's sad"
}
Was it helpful?

Solution

A common rule for a server is to be generous in what you accept and strict in what you produce.

I'd prefer to accept such an input and get as much as possible out of it. Any unknown property of the input would be ignored.

OTHER TIPS

HTTP 422: Unprocessable Entity

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

A common practice is NOT to add valid message properties if the message also contains invalid properties.

When answering that question you have to consider versioning too. Do you support old clients that send fewer arguments than expected - that could work with suitable defaults. Or do you have more than one server implementation such that newer clients would be sending too many arguments for an older server implementation? If the answer is yes to one of these then you have to be careful about rejecting inputs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top