What Content-Type should a 204 No Response use?
Asked Answered
H

2

58

When building a RESTful HTTP service, and a Response is given with a 204 No Content (e.g. after the Consumer issues a DELETE Request), what Content-Type should the Response include?

Should it be omitted? Is there a preferred Content-Type? Should it be the same Content-Type of a GET Request to the same Resource? Does it not matter whatsoever?

Hep answered 9/1, 2014 at 19:27 Comment(2)
Related to, or possible duplicate of: Should content-type header be present when the message body is emptyReade
Yes, the other question duplicated my question 15 months after it was asked.Hep
Q
70

You have Content-Type when you have content and even then it's optional:

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body.

(from RFC2616, §7.2.1, please note the use of SHOULD).

Since there is no content, then there is no reason to specify a Content-Type header.

Quadrangle answered 9/1, 2014 at 19:30 Comment(1)
I believe tools.ietf.org/html/rfc2616#section-7.2.1 is what you are looking for.Hep
S
11

Well, there's a little more to the story though, and it involves when the real-world (browsers) meet the theoretical world (specifications). Seems Firefox (ver 38 as I write this) has a bug where the browser attempts to parse the content as xml if there's no content-type header, EVEN WHEN the response code is 204 (no content). See https://bugzilla.mozilla.org/show_bug.cgi?id=521301

So, while a content type header doesn't make much sense when there's no content (and there MUST NOT be content on a 204) it seems to make sense to return some header anyway. And I don't see where that would be a violation of the spec.

Shaveling answered 18/6, 2015 at 16:1 Comment(3)
That bug seems to be fixed now.Lippert
Thanks for the update. Though I do note that they say "The XHR's responseXML will be null and a parsing failure will simply be logged to the web console instead." so we'd still be creating console spew... I'd stick w/still returning the Content-Type header.Shaveling
The error doesn't seems to occur when browser get the response, but when browser try to get the XML with responseXML. If that's the case, I wonder whether it's even a bug.Lippert

© 2022 - 2024 — McMap. All rights reserved.