Andy Fusniak
Andy Fusniak

Reputation: 1638

Should Content-Type header be set for 404 and 204 responses?

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?

Upvotes: 7

Views: 5762

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123270

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.

Upvotes: 8

Related Questions