HTTP response for invalid Accept header

My RESTFul API can only respond to JSON-encoded data (i.e. all my headers have Content-Type: application/json). What should I return if the request has a header Acceptthat does not allow JSON (e.g. Accept: text/html)? Should I just return 400 Bad Requestwith an explanation in the body, or is there a more specific status code for this exception?

Please note that this is different from unsupported request content types .

+5
source share
2 answers

Do not worry.

There will be times when consumers of your service do not want to set this header, for example. when using cURLor file_get_contents()in PHP.

API , JSON, .

, . /path/to/resource.json?a=b /path/to/resource.xml?a=b JSON XML .

, Accept , .

+3

:

HTTP/1.0:

a 406 Not Acceptable - , , .

HTTP/1.1, "" , ,

RFC 2616 Sec 10.4.7

 Note: HTTP/1.1 servers are allowed to return responses which are
      not acceptable according to the accept headers sent in the
      request. In some cases, this may even be preferable to sending a
      406 response. User agents are encouraged to inspect the headers of
      an incoming response to determine if it is acceptable.

, , , , @Jack. .

+9

All Articles