Or Smith
Or Smith

Reputation: 3606

Parse request with JSON.parse

I try to parse a request with node.js, So I build the next:

 app.get('/download', function(req, res){
      var parseJson = JSON.parse(req);
      var file = parseJson.fileName;
      res.download(file); // Set disposition and send it.
    });

And I send a request with the Advanced Rest Client:

URL: http://localhost:5000/download

Method: GET

Raw arguments: fileInfo: "C://1//239698_n.jpg"

and it gives me the next error:

SyntaxError: Unexpected token o
    at Object.parse (native)
    at port (c:\Node\nodejs\express.js:13:24)
    at callbacks (c:\Node\nodejs\node_modules\express\lib\router\index.js:164:37)
    at param (c:\Node\nodejs\node_modules\express\lib\router\index.js:138:11)
    at pass (c:\Node\nodejs\node_modules\express\lib\router\index.js:145:5)
    at Router._dispatch (c:\Node\nodejs\node_modules\express\lib\router\index.js:173:5)
    at Object.router (c:\Node\nodejs\node_modules\express\lib\router\index.js:33:10)

What can be the reson?

Upvotes: 0

Views: 246

Answers (1)

Jason
Jason

Reputation: 13766

You're not sending JSON. You're sending querystring formatted arguments. You'll want to use querystring.parse() or node-querystring or, shameless plug alert, my module that is format agnostic, objectifier.

Upvotes: 1

Related Questions