HTTP headers
HTTP-заголовки
These are named fields sent with an HTTP request or response, separate from its body. They tell the recipient things like what format the data is in or how long a response can be cached.
Also known as: Content-Type, заголовки
What HTTP headers do
The body of an HTTP message carries the data you're sending. Headers give the recipient information it needs to handle the message. For instance, the same server can send JSON from one endpoint and an image from another. The Content-Type header identifies which kind of data the recipient got. A message can have headers without a body: a GET request can say that the client prefers JSON even though it isn't sending any JSON itself.
What they look like
In HTTP/1.1, headers appear as Name: value lines after the request line or status line. A blank line ends the header section, and the body, if present, follows. HTTP/2 and HTTP/3 encode fields in binary rather than sending literal text lines. Header names are case-insensitive, so Content-Type and content-type refer to the same field. Value syntax depends on the field. Some fields support comma-separated lists, but you shouldn't join every repeated field that way: multiple Set-Cookie fields need to stay separate.
Example: creating a resource
POST /books HTTP/1.1
Host: api.example.test
Content-Type: application/json
Accept: application/json
Content-Length: 16
{"title":"Dune"}
Here Content-Type describes the request body and Accept tells the server what format the client wants back. Content-Length counts bytes, not characters; this example has 16 bytes with no trailing newline. Setting Content-Type doesn't serialize an object for you. The client still needs to send valid JSON, and the server can reject content it can't parse or doesn't support.
Where you'll see them
- Authentication:
Authorizationcan carry credentials such as a bearer token. Redact credentials rather than logging all request headers as-is. - Sessions: A server can send
Set-Cookiein a response. The browser then sends applicable stored cookies in theCookieheader on later requests. - Caching: A server can send an
ETagthat identifies a version of a response. On a later GET, the client can send that tag inIf-None-Match. If it still matches, the server can reply with304 Not Modifiedand no body, allowing the client to reuse its cached copy.
What interviewers ask
You might be asked to distinguish 415 Unsupported Media Type from 406 Not Acceptable. A 415 points to what the client sent, such as a request body in a format the endpoint doesn't support. A 406 can happen when the server can't produce a response that meets the client's stated preferences and chooses not to send a fallback. When debugging, inspect the actual request and response in the browser's Network panel or with a command-line HTTP client. Check the outgoing Content-Type for a request-body problem and Accept for a response-format problem.