Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 458
Brad Avatar asked Sep 30 '14 01:09

Brad


1 Answers

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.

like image 152
David Rissato Cruz Avatar answered Sep 29 '22 15:09

David Rissato Cruz