简体   繁体   中英

Is Restlet returning 415 Unsupported Media Type when it should return 400 Bad Request?

I'm using Restlet 2.1 with jackson to build a json REST api.

When I make a request with the expected content type but a malformed body, I get back a 415 "Unsuppored Media Type" status code. I think the correct error code should be 400 "Bad Request".

Apparently the mixup happens when Jackson tries and fails to decode the garbage.

I'll try to make the case more clear with some code:

// java method mapping
@Post("json")
public Project create(Project project) {

The service invocation with curl

$ curl -i -XPOST -H 'content-type: application/json' -d '{xgarbage}' http://localhost:8080/projects HTTP/1.1 415 Unsupported Media Type

And a fragmente of the stack trace os recorde in the logs:

Nov 29, 2010 9:51:56 PM org.restlet.ext.jackson.JacksonRepresentation getObject
WARNING: Unable to parse the object with Jackson.
org.codehaus.jackson.JsonParseException: Unexpected character ('x' (code 120)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: java.io.ByteArrayInputStream@693e4a5a; line: 1, column: 2]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:929)

The actual implementation of the service is never hit, so somewhere a decision is made to map the garbled content to a 415.

Now, my question is: is this correct? If I'm reading correctly the following quotes from the book "RESTful Web Services", it is not, but I'm open to corrections.

[400 Bad Request] It's commonly used when the client submits a representation along with a PUT or POST request, and the representation is in the right format, but it doesn't make any sense.

.

[415 Unsupported Media Type] If the client sends a document that's got the right media type but the wrong format (such as an XML document written in the wrong vocabulary), a better response is the more generic 400 (“Bad Request”)

Right or wrong, I'd prefer to return a 400.

Is there a way to change the behaviour without renouncing at the auto-magic serialisation provided by Jackson?

Any help is greatly appreciated, thanks!

415 is correct, as the request is NOT in the right format if it is corrupted in anyway. For example non-parseable JSON or XML. Malformed JSON or XML is NOT JSON or XML by definition, and thus is an unsupported media type, there is no way for Jackson to know that is is supposed to be JSON, it just knows that it isn't JSON that it can parse.

The offical documentation is really clear about this.

10.4.16 415 Unsupported Media Type

The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.

you said, hey this is JSON, and it isn't so the server says, hey, what I got isn't JSON and not supported by this resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM