What are HTTP headers?
HTTP headers are service information sent in an HTTP request and HTTP response that helps the client and server understand each other correctly.
In simple terms: HTTP headers are the "explanations" attached to a request or response.
Why HTTP headers are needed
Headers are needed to communicate:
- what data is being sent
- in what format
- how to process it
- who the sender is
- whether authorization is required
- whether the response can be cached
Without headers, the server and client would see just a set of bytes and would not understand their meaning.
Where HTTP headers are located
Headers exist:
- in the HTTP request
- in the HTTP response
They come:
- after the start line
- before the message body
Examples of HTTP headers (simple and important)
Content-Type
Shows what data is being sent:
text/htmlapplication/jsonimage/png
The server and client understand how to interpret the data.
Authorization
Used for authentication:
- tokens
- access keys
Tells the server who is making the request.
Accept
Shows what data formats the client is willing to accept:
- JSON
- HTML
- XML
User-Agent
Information about the client:
- the browser
- the operating system
- the application
Cache-Control
Controls caching:
- whether the response can be stored
- how long it stays valid
Request headers and response headers
Request headers
Describe:
- the client
- the request's format
- access conditions
An example of what they mean:
"I am a browser, I want JSON, here is my token"
Response headers
Describe:
- the response's data
- the format
- processing rules
An example of what they mean:
"Here is the data, it's JSON, you can cache it for 5 minutes"
What matters for an interview
- headers are not the request body
- they do not contain the main data
- they control how the transfer behaves
- one request or response can have many headers
Real-life example
HTTP headers are like stickers on a package:
- "Fragile"
- "Handle with care"
- "Urgent"
- "Where to deliver"
The package itself is the body, and the stickers are the headers.
Short answer for an interview
Remember this wording:
HTTP headers are the service fields of an HTTP request and response that carry meta-information about the data, the client, and the rules for processing the message.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.