We use req.headers to see extra information sent by the browser or client with a web request. This helps the server understand details like language, type of data, or authorization.
req.headers for HTTP headers in Express
const headers = req.headers; // headers is an object with all HTTP headers in lowercase keys const userAgent = req.headers['user-agent'];
req.headers is a plain JavaScript object where each key is a header name in lowercase.
You access headers using bracket notation with the header name as a string, for example req.headers['content-type'].
const host = req.headers['host'];const contentType = req.headers['content-type'];const auth = req.headers['authorization'];This Express app reads the User-Agent and Accept headers from the incoming request. It then sends back a message showing these values.
Try opening http://localhost:3000 in a browser to see your browser info and accepted content types.
import express from 'express'; const app = express(); app.get('/', (req, res) => { const userAgent = req.headers['user-agent']; const accept = req.headers['accept']; res.send(`Your browser info: ${userAgent}\nYou accept: ${accept}`); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Header names are always lowercase in req.headers, even if the client sent them with uppercase letters.
Some headers like cookie or authorization may contain sensitive info, so handle them carefully.
If a header is not sent by the client, accessing it returns undefined.
req.headers lets you read all HTTP headers sent by the client.
Headers are stored as lowercase keys in a JavaScript object.
You can use this info to customize responses or check client details.
Practice
req.headers represent in an Express app?Solution
Step 1: Understand the role of
req.headersreq.headersholds all headers sent by the client in the HTTP request.Step 2: Identify what
It is an object with keys as header names (lowercase) and values as header values.req.headerscontainsFinal Answer:
An object containing all HTTP headers sent by the client -> Option BQuick Check:
req.headers= client headers [OK]
- Thinking req.headers is a function
- Confusing headers with URL or status code
- Expecting headers keys to be uppercase
req.headers?Solution
Step 1: Recall header key casing in
Headers keys are always lowercase inreq.headersreq.headers.Step 2: Choose the correct syntax to access the header
Use bracket notation with lowercase key:req.headers['content-type'].Final Answer:
req.headers['content-type'] -> Option DQuick Check:
Headers keys are lowercase strings [OK]
- Using camelCase or uppercase keys
- Using dot notation with hyphenated keys
- Assuming headers keys keep original case
app.get('/', (req, res) => {
const userAgent = req.headers['user-agent'];
res.send(userAgent);
});What will the server send back to the client?
Solution
Step 1: Identify what
This header holds the client's browser and system info string.req.headers['user-agent']containsStep 2: Understand the response sent
The server sends back this string as the response body usingres.send().Final Answer:
The client's browser user agent string -> Option CQuick Check:
req.headers['user-agent']= client info [OK]
- Thinking it returns server info
- Assuming 'user-agent' header is invalid
- Expecting an error or empty response
app.get('/data', (req, res) => {
const auth = req.headers.Authorization;
if (!auth) {
res.status(401).send('Unauthorized');
} else {
res.send('Access granted');
}
});Solution
Step 1: Check header key casing in
All header keys are lowercase, so 'Authorization' is incorrect.req.headersStep 2: Correct the header key usage
Usereq.headers.authorizationto access the Authorization header properly.Final Answer:
Header keys in req.headers are lowercase, so 'Authorization' should be 'authorization' -> Option AQuick Check:
Headers keys are lowercase strings [OK]
- Using uppercase or camelCase header keys
- Thinking headers are case-sensitive as sent
- Believing res.status() and res.send() can't chain
Solution
Step 1: Understand header key casing in
Headers keys are lowercase, so usereq.headersreq.headers['x-client-id'].Step 2: Verify the correct way to access custom headers
const clientId = req.headers['x-client-id']; if(clientId) res.send(`Client ID: ${clientId}`); else res.send('No Client ID'); uses lowercase key and bracket notation correctly to get the header value.Step 3: Check other options
const clientId = req.headers['X-Client-Id']; if(clientId) res.send(`Client ID: ${clientId}`); else res.send('No Client ID'); uses uppercase keys (wrong), B uses camelCase (wrong), D usesreq.headers.get('X-Client-Id')which doesn't exist on req.headers.Final Answer:
const clientId = req.headers['x-client-id']; if(clientId) res.send(`Client ID: ${clientId}`); else res.send('No Client ID'); -> Option AQuick Check:
Headers keys lowercase in req.headers [OK]
- Using uppercase or camelCase keys
- Confusing req.headers with req.get()
- Accessing headers with dot notation for hyphenated names
Ready to prove it?
Pass the test and this pattern moves unknown
