HTTP Error Codes 406 and 415

I am writing a web service that only accepts json and also only outputs json.

Therefore, I need to return the corresponding status code if any other format is requested.

I seem to have two options:

  • 406 - Not Allowed
  • 415 - Unsupported media type

It would be great if someone could tell me about the semantics of the two codes.

+49
web-services
Jul 20 '10 at 21:24
source share
5 answers

406 is returned by the server when it cannot respond based on the acceptance of the request headers (i.e. they have an Accept header that says they only want XML).

415 is returned by the server when the object sent in the request (content in POST or PUT) has an unsupported media type (i.e., sent XML).

so .. 406 when you cannot send what they want, 415 when they send what you do not want.

Hope this helps!

+96
Jul 20 2018-10-21
source
  • 406, if the Accept header was sent, you cannot fill out.
  • 415, if Content-Type sent, you cannot use.
+31
Jul 20 2018-10-20T00:
source

To quote RFC2616 :

406 Not acceptable

The resource specified in the request is capable of generating a response objects that have content characteristics that are unacceptable in accordance with the sent headers in the request.

When a client requests your service, check which Accept* headers are sent; if it does not match application/json (or a wildcard such as */* ), return this. The answer should point to "we only serve JSON here."

415 Unsupported media type

The server refuses to service the request because the request object is not supported in the format of the requested resource for the requested method.

Just returning 415 Unsupported Media Type should be the minimum answer for "the client sent something that is not JSON, cannot work with it"; not sure if there is a header indicating "you need to send JSON"

+5
Jul 20 2018-10-21
source

406 is used when a client requests a response in an unsupported content type (in your case, nothing but JSON) using the Accept header. 415, on the other hand, is used when client POST or PUT data is in an unsupported content type.

In a nutshell: use 406 if you cannot output in the expected format and use 415 if you do not support the input format.

See RFC 2616 for its definitions: 406 and 415.

+3
Jul 20 2018-10-21
source

RFC2616 will help you!

http://www.rfc2616.com/#10.4.7

http://www.rfc2616.com/#10.4.16

I would choose 415, this is good for your description.

Edit: Oh. IC. "The request object is in a format not supported by the requested resource." Therefore, if you have a request with content and this content is of the wrong type, you should throw 415 response.

-2
Jul 20 '10 at 21:33
source



All Articles