0
0
Expressframework~20 mins

req.headers for HTTP headers in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Headers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What does req.headers contain in Express?

In an Express app, what kind of data does req.headers hold?

Express
app.get('/', (req, res) => {
  res.json(req.headers);
});
AAn array of header names only.
BA string containing all headers concatenated with commas.
CAn object with all HTTP headers sent by the client, with header names as keys and their values as strings.
DA function to retrieve headers by name.
Attempts:
2 left
💡 Hint

Think about how HTTP headers are structured and how Express exposes them.

📝 Syntax
intermediate
1:30remaining
Accessing a specific header value from req.headers

Which code correctly gets the value of the content-type header from req.headers in Express?

Express
app.post('/data', (req, res) => {
  const contentType = /* fill here */;
  res.send(contentType);
});
Areq.headers['content-type']
Breq.headers['Content-Type']
Creq.headers.contentType
Dreq.get('content-type')
Attempts:
2 left
💡 Hint

Header names in req.headers are all lowercase.

🔧 Debug
advanced
2:00remaining
Why does req.headers['Authorization'] return undefined?

Given this code snippet, why might req.headers['Authorization'] be undefined?

app.get('/secret', (req, res) => {
  const auth = req.headers['Authorization'];
  res.send(auth || 'No auth');
});
AHeader names in <code>req.headers</code> are lowercase, so it should be <code>req.headers['authorization']</code>.
BThe client did not send the Authorization header, so it is undefined.
CExpress blocks the Authorization header by default for security.
DYou must use <code>req.get('Authorization')</code> instead of <code>req.headers</code>.
Attempts:
2 left
💡 Hint

Check how Express normalizes header names in req.headers.

state_output
advanced
1:30remaining
What is the output of this Express route logging req.headers?

Consider this Express route:

app.get('/test', (req, res) => {
  console.log(req.headers);
  res.send('ok');
});

If a client sends a request with headers:

{
  'Host': 'example.com',
  'User-Agent': 'curl/7.68.0',
  'Accept': '*/*'
}

What will be logged to the console?

A'Host: example.com, User-Agent: curl/7.68.0, Accept: */*'
B{ Host: 'example.com', User-Agent: 'curl/7.68.0', Accept: '*/*' }
C[ 'Host', 'User-Agent', 'Accept' ]
D{ host: 'example.com', 'user-agent': 'curl/7.68.0', accept: '*/*' }
Attempts:
2 left
💡 Hint

Remember how Express formats header names in req.headers.

🧠 Conceptual
expert
2:30remaining
Why should you avoid modifying req.headers directly in Express?

In Express, what is the main reason you should not change req.headers directly inside a middleware or route handler?

ABecause <code>req.headers</code> is a read-only object and modifying it throws an error.
BBecause modifying <code>req.headers</code> can cause unexpected behavior in downstream middleware or routing logic that rely on original headers.
CBecause Express automatically resets <code>req.headers</code> after each middleware.
DBecause headers must be modified only on the response object, not the request.
Attempts:
2 left
💡 Hint

Think about how middleware and routing depend on request data.