Brad
Brad

Reputation: 163262

Express URL parameter feature doesn't decode plus (+) as space

When using Express' URL parameter functionality, it seems that parameters are automatically decoded. That is, percent-encoded entities are resolved to their normal form. %20 is replaced with a space.

However, a plus + is not replaced with a space. This is presumably because Express is using decodeURIComponent() internally, which also does not replace plus + with a space. Simple example code:

app.get('/:sourceFile', function (req, res, next) {
    console.log(req.params.sourceFile);
});

If you request /test%20test, then you get test test on the console. If you request /test+test, then you get test+test on the console.

Is there a way to change this mode of operation in Express 4? Is this a bug?

Upvotes: 11

Views: 7632

Answers (1)

David Rissato Cruz
David Rissato Cruz

Reputation: 3647

You are trying to use + to represent a space in the "URI part" of your request. You can't do that. A plus sign is translated to a space only in query strings.

It is not a bug. In URI specs (page 12/13 https://www.rfc-editor.org/rfc/rfc3986), plus sign is a reserved character, not meant to be translated as a space.

Upvotes: 4

Related Questions