0

For exemple this is my server with a simple API :

var express = require('express');
var rzServer = express();
rzServer.use(bodyParser.urlencoded({extended:true}));
rzServer.use(bodyParser.json());

app.get('/url', function(req, res) {
   console.log(req.query.data); // String
   console.log(JSON.parse(req.query.date)); // Object
});

req.query.data is interpreted as a string but it's a JSON Object.

Is it possible with the body-parser package to parse the querystring ?

Thanks.

3
  • 1
    Body parser only parses the incoming body on POST requests. Commented Jul 21, 2016 at 7:59
  • Okay, That answers my question, Thanks ^^ Commented Jul 21, 2016 at 8:01
  • 1
    JSON.parse is your friend Commented Jul 21, 2016 at 8:10

1 Answer 1

3

body-parser is a middleware to parse body (it's its name). If you want to parse the query string, so you need another middleware for that.

Another thing : GET requests normally don't take any JSON parameters (no body). If you need to send a true JSON, perhaps you're not using the good HTTP method. Try to use a POST request, or create a true query string (http://expressjs.com/fr/api.html#req.query).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.