HTTP Response Headers
For the last couple of weeks I’ve been working on creating a simple Java HTTP server from scratch. And let me tell you, that simple server is not so simple to create. My server accepts client’s requests, routes them accordingly, and sends the response. Server responses usually contain headers and body. The headers allow server to pass some additional information in the response. There are a lot of common HTTP headers. But I had no idea of which headers I should include in responses and why, so I did a little research on this topic. I’ll share my findings in this post.
Generally speaking no headers are required in all cases. And what is a required header anyway? A required header is a header that MUST be included into response according to specs in RFCs (sets of specifications called Requests for Comments published by World Wide Web Consortium). So once again, there are no headers that are required in all cases, but there are headers that are required in some cases. And of course there are headers that are better to be used just because it is a good practice to do so.
The headers that should be added to response are Date, Content-Length, and Content-Type.
Date response HTTP header contains the date and time at which the response was originated.
Date: Thu, 31 Jan 2019 11:08:13 -0500According to specs, Date header MUST be sent if the origin server is capable of providing a “reasonable approximation” of the date in UTC. It’s hard to imagine a modern server that is not capable of providing a “reasonable approximation” of the date in UTC. So on the one hand, this header is a required one, but on the other hand, who knows what your server is capable and not capable of :)
The Content-Length header indicates the size of the entity-body, in bytes, sent to the recipient.
Content-Length: 379The Content-Length header is a very useful one for two main reasons. First, if receiving party knows exactly the length of the content to be received, it can finish receiving as soon as that amount of bytes is reached and reuse the connection for another request. Otherwise, if content-length is unknown, it will be just sitting there waiting to receive content until server closes the connection. Second, knowing the length of the data to be received allows client to allocate the required memory and disk space at once, which benefits the performance.
The Content-Type header indicates the media type of the resource.
Content-Type: text/htmlEven though this header is not required, but knowing the content-type allows to interpret data correctly. There are more then 1500 media types which can be set in Content-Type header, so knowing the type and not guessing it really helps.
Get obsessive compulsive Order’s stories in your inbox
Join Medium for free to get updates from this writer.
Another header which is also widely used is Server.
Server: Apache/2.4.1 (Unix)This header provides information about the software used by the server to handle the request. This header is used for surveys on how much market share each server has. However, the usage of this header is often debated. Some developers see using this header as a security issue.
There are headers which are required in some cases. Let’s see one example.
The Allow header gives a list of methods supported by a resource.
Allow: GET, POST, HEADThe server MUST generate an Allow field in a 405 (Method Not Allowed) response and MAY do so in any other response. An empty Allow field value indicates that the resource allows no methods, which might occur in a 405 response if the resource has been temporarily disabled by configuration.
Just to mention a couple of other headers that became required in some cases:
- WWW-Authenticate header
A server generating a 401 (Unauthorized) response MUST send a WWW-Authenticate header field containing at least one challenge. A server MAY generate a WWW-Authenticate header field in other response messages to indicate that supplying credentials (or different credentials) might affect the response. - Connection
A server that does not support persistent connections MUST send the “close” connection option in every response message that does not have a 1xx (Informational) status code.
Then, there are headers to be included in response for a specific purpose. Like headers to improve performance via caching, or headers to prevent security issues, or headers needed for specific browsers to interpret the response correctly. But that’s worth a separate blog post.
Useful resources:









