Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Content-Type should a 204 No Response use?

Tags:

rest

http

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?

like image 261
Thomas Hunter II Avatar asked Jan 09 '14 19:01

Thomas Hunter II


People also ask

How do you handle a 204 response?

By default, 204 (No Content) the response is cacheable. If caching needs to be overridden then the response must include cache respective cache headers. For example, you may want to return status 204 (No Content) in UPDATE operations where request payload is large enough not to transport back and forth.

Can 204 response have body?

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

When should I use 200 or 204 status code?

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.


2 Answers

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.

like image 197
Stefano Sanfilippo Avatar answered Oct 07 '22 23:10

Stefano Sanfilippo


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.

like image 44
Walden Leverich Avatar answered Oct 07 '22 23:10

Walden Leverich