HTTP Header

May 20, 2023

HTTP (Hypertext Transfer Protocol) is the foundation of data communication between the client and the server on the World Wide Web. When a user requests a web page, the request is sent through HTTP to the server, which then responds with the content of the requested page. An HTTP header is a part of an HTTP request or response that contains additional information about the requested resource, the client or server, or the transfer of data.

HTTP headers are comprised of a set of keys and values, separated by a colon, that are sent along with an HTTP request or response. The keys describe the information that is being sent, while the values provide the specific details. HTTP headers can be divided into two main types: request headers and response headers.

Request Headers

Request headers are sent from the client to the server as part of an HTTP request. They provide additional information about the resource being requested or the client making the request. Request headers can be categorized into the following groups:

General Headers

General headers are used in both requests and responses and provide information about the message as a whole. They include:

  • Cache-Control: Specifies caching directives that must be obeyed by all caching mechanisms along the request/response chain.
  • Connection: Controls whether the network connection remains open after the current transaction finishes.
  • Date: Contains the date and time at which the message was originated.
  • Pragma: Provides backwards compatibility with HTTP/1.0 caches.

Request Headers

Request headers are used only in HTTP requests and provide additional information about the requested resource. They include:

  • Accept: Lists the MIME types of response content that are acceptable for the client.
  • Accept-Charset: Lists the character sets that are acceptable for the client.
  • Accept-Encoding: Lists the content encodings that are acceptable for the client.
  • Authorization: Contains the credentials necessary to authenticate the client with the server.
  • Cookie: Contains the cookie data previously sent by the server with the Set-Cookie header.
  • host: Specifies the Internet host and port number for the resource being requested.
  • Referer: Contains the URL of the resource from which the request was initiated.
  • User-Agent: Contains information about the client software.

Response Headers

Response headers are sent from the server to the client as part of an HTTP response. They provide additional information about the resource being returned or the server providing the response. Response headers can be categorized into the following groups:

General Headers

General headers are used in both requests and responses and provide information about the message as a whole. They include:

  • Cache-Control: Specifies caching directives that must be obeyed by all caching mechanisms along the request/response chain.
  • Connection: Controls whether the network connection remains open after the current transaction finishes.
  • Date: Contains the date and time at which the message was originated.
  • Pragma: Provides backwards compatibility with HTTP/1.0 caches.
  • Trailer: Specifies the header fields that are present in the trailer of a message that is encoded with chunked transfer coding.

Response Headers

Response headers are used only in HTTP responses and provide additional information about the resource being returned. They include:

  • Accept-Ranges: Indicates the range of byte offsets that are acceptable for the requested resource.
  • Age: Indicates the time in seconds that the response has been cached by an intermediate cache.
  • Content-Encoding: Indicates the encoding applied to the message body before transmission.
  • Content-Length: Indicates the size of the message body in bytes.
  • Content-Type: Indicates the MIME type of the message body.
  • Expires: Indicates the date and time after which the response is considered stale.
  • Last-Modified: Indicates the last time the resource was modified.

HTTP headers are an important aspect of HTTP communication since they provide additional information about the request or response beyond the data communicated in the request or response body. They also allow servers to provide instructions to clients and intermediaries regarding how the data should be processed or cached. Lastly, headers can be used to control access to resources by specifying authentication credentials or other security measures.