Do we need to follow http accept header on an error?

136 views Asked by At

Does HTTP require that the server return one of the accept header media types on an error? For instance we perform a POST with the accept header application/vnd.foo.bar-v1+json

and error occurs and the result is a 422. Do we need to return the application/vnd.foo.bar-v1+json or can we return a different media type since an error occured? Could I return a application/vnd.foo.errors-v1+json?

Looking a W3 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html I did not see an outright explanation.

1

There are 1 answers

0
kuporific On BEST ANSWER

From https://www.rfc-editor.org/rfc/rfc7231#section-6.5:

the server SHOULD send a representation containing an explanation of the error situation

What this means is that the body of the response should contain the error, and the Content-Type identifies its type. For example, by default Tomcat returns an HTML page, so the Content-Type is set appropriately (to text/html, I believe).

If your error has the media type application/vnd.foo.bar-v1+json then you should set it as the Content-Type.