Express req.get() Method
What you’ll learn
- How to read request headers using
req.get(). - How to access common headers like Authorization and Content-Type.
- What
req.get()returns for missing headers. - How to avoid header-parsing mistakes in middleware.
Usage syntax
javascript
req.get(field)
req.header(field)1
Read Authorization header
javascript
app.get('/secure', function (req, res) {
var auth = req.get('Authorization');
if (!auth) return res.status(401).send('Missing Authorization header');
res.send('Authorization header received');
});2
Validate Content-Type header
javascript
app.post('/api/data', function (req, res) {
var contentType = req.get('Content-Type') || '';
if (contentType.indexOf('application/json') !== 0) {
return res.status(415).send('Unsupported Media Type');
}
res.send('Valid content type');
});❓ FAQ
It reads a specific incoming HTTP request header value by name.
No. req.get() header name matching is case-insensitive.
req.get() returns undefined when the requested header is not present.
Yes. req.header() is an alias of req.get() in Express.
Yes. It is commonly used to read Authorization, Content-Type, and custom headers.
Did you know?
req.get(headerName) is case-insensitive and returns the value of a request header or undefined if missing.
4 people found this page helpful
