Skip to content

Express 4 and 5 req.get doesnt return header if not set in lowercase #5288

Open
@fbritoferreira

Description

@fbritoferreira

In the documentation, it's stated that req.get is intended to be case-insensitive. However, it appears that when you set a custom header in a middleware, this case-insensitivity breaks if the header is not in lowercase. Should this behavior be corrected, or should the guidance be to always set headers in lowercase? Perhaps, adding a setHeader function in the request object could be a solution to this issue.

The problem seems to originate from this part of the code: https://github.com/expressjs/express/blob/master/lib/request.js#L82, where the headers are not being converted to lowercase, while the input is.

Example code

const express = require('express');
const app = express();

app.use((req, res, next) => {
    req.headers['X-filipe'] = 'filipe';
    next();
});

app.get('/', (req, res) => {
    res.json({ header: req.get('x-filipe') }); 
});

app.listen(3000, () => {
    console.log('Server is listening on port 3000');
});

Expected output:

{"header": "filipe"}

This adjustment ensures that the headers are consistently in lowercase, aligning with the case-insensitive nature of req.get.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions