sajad torkamani

What is a HTTP header?

A HTTP header is a piece of metadata sent as part of an HTTP request or response.

It’s made up of a name and a value, and is used to either:

  1. Help a server process an incoming HTTP request, or
  2. Help a user agent like a web browser process a HTTP response

HTTP headers in a request

When a web browser sends a request to a server, it can include headers to give the server extra details such as:

  • User-Agent: Tell the server which browser the request is coming from and what version of the browser. The server can then this info to render different content (e.g.., it can serve iOS specific content if the request is coming from an iPhone).
  • Accept: Tell the server what response formats are acceptable as a response.
  • Authorization: Pass authorization details so the server can authenticate the request.

Example request headers

GET /index.html HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Accept: text/html

HTTP headers in a response

When a server responds to a request, it can include its own headers to help the web browser process the request:

  • Content-Type: Tell the browser what type of content the response is (application/json, application/pdf, etc) so the browser can process it properly.
  • Cache-Control: Instruct the browser how to cache the response.
  • Set-Cookie: Instruct the browser to save some cookie data.

Example response headers

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1024
Cache-Control: no-cache
Tagged: HTTP