Parsing for req.query changes + signs to spaces

You can observe this with a very simple app:

var app = express();

app.get('/', function (req, res) {
    console.log(req.originalUrl);
    console.log(req.query);
    res.send(req.query);
});

app.listen(7000);

If you go to http://localhost:7000/?thing=this+that, express receives the plus sign and still has it in originalUrl, but req.query contains { thing: 'this that' }.

This makes it so that if a float in the form of 1.7976931348623157e+308 is used in a query parameter, it cannot be parsed correctly in express.

Author: Fantashit

1 thought on “Parsing for req.query changes + signs to spaces

  1. Actually, scratch that, you can get the parse you desire with qs in our own parser function. The replacement is happening at

    https://github.com/ljharb/qs/blob/037f3686e8f8eee456cf958c39ffd8a967d4ead5/lib/utils.js#L112

    var express = require('express');
    var qs = require('qs');
    
    var app = express();
    
    app.set('query parser', function (str) {
      return qs.parse(str, { decode: function (s) { return decodeURIComponent(s); } });
    });
    
    app.get('/', function (req, res) {
        console.log(req.originalUrl);
        console.log(req.query);
        res.send(req.query);
    });
    
    app.listen(7000);

    I hope that helps!

Comments are closed.