Kian Soltani
Kian Soltani

Reputation: 97

Body-parser fails to/do not parse urlencoded parameters from GET request

I'm creating a web platform with a Nodejs server. I'm trying to retrieve urlencoded data sent from my front but can't manage to.

How I send the GET request :

xhr.open("GET", address + "?limit=1&offset=1",true);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(null);
xhr.addEventListener("readystatechange", processRequest, false);

On the server side :

const bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: true });

app.get('/guid_list', urlencodedParser, function (req, res) {
  console.log(req.body.limit);
  console.log(req.body.offset);
  var headerjwt = HeaderGetJWT(req);
...
}

I have no problem retrieving the jwt token I'm sending, but always get undefined for urlencoded parameters. I was wondering if I should use multipart content type instead, since I'm sending both a token and urlencoded data ? And maybe "multer" module in that case, since body-Parser does not support that content type.

Upvotes: 0

Views: 302

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30715

I would suggest accessing your parameters in Node.js as follows (since they are being passed as query parameters):

app.get('/guid_list', parser, function (req, res) {
    console.log("req.query.limit:", req.query.limit);
    console.log("req.query.offset:", req.query.offset);

});

or just log all parameters:

app.get('/guid_list', parser, function (req, res) {
    console.log("req.query:", req.query);
});

Upvotes: 1

Related Questions