6

I plan to use a HTTP HEAD /products/{product_id} request as a low bandwidth way to determine if a product exists.

If it does, I intend to return 204 No Content. If it does not, I intend to return 404 Not Found.

In my HTTP service there is some middleware that adds the Content-Type: application/json.

The final response is

HTTP/1.1 204 No Content
Content-Type: application/json
Vary: Origin
Date: Wed, 07 Aug 2019 16:50:08 GMT

All other resources return JSON responses. Should I remove the Content-Type header and does it matter for 204 and 404 type responses?

Andy Fusniak
  • 1,548
  • 1
  • 17
  • 28
  • 2
    A response with no content (e.g. 204) does not need a content-type by definition. If it is provided, it will be ignored. – Adrian Aug 07 '19 at 17:35
  • Using `HEAD` is a bit of a red herring here. [HEAD is identical to GET, except that no content is returned](https://stackoverflow.com/a/25653119/13860). So if you would respond to a GET request with content, then, by spec, you SHOULD also include all the same headers, including `Content-Type`, for your HEAD response--just without content. if you would send no content with a GET request, then no Content-Type is needed for either GET or HEAD requests. – Jonathan Hall Aug 08 '19 at 07:44

1 Answers1

7

The Content-Type header is used by the client to interpret the content. If there is no content (as with a HEAD request in general or as with status code 204 for other requests) there does not need to be a Content-Type header since no interpretation of the (zero) content need to be done. But it also does not harm, i.e. it will simply be ignored.

If you send content (which is not allowed with HEAD) with status code 404 then you should also send a Content-Type header, if you don't send a content (i.e. Content-length: 0) then you don't need to add such a header.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172