1

I have the following test code,

app.use(function(req,res,next)
{

console.log("Params : " + req.params);

console.log("Path : " + req.path);

console.log("Query :" +req.query);


for(var params in req.query)
{

    console.log(params);
}

res.end("processed");

});

whenever I generate a request with the following url

http://127.0.0.1:8080/path/resource/myname?user=alanz&password=alpacasocks

I obtain the output

Params : [object Object]
Path : /path/resource/name
Query :[object Object]
user

It does not seem to detect the second query variable. Generating the request with curl.

Alan
  • 1,134
  • 2
  • 13
  • 25
  • What is the value of `user` param? Have you tried with a browser instead of curl? – Aleksandr M Apr 09 '15 at 13:39
  • I have not tried a browser since I am using cloud9 (unsure if it allows public connections) but I will when I get a chance. I tried the solutions below and cannot get it to work, however I am starting to suspect it is an issue with curl rather than node – Alan Apr 09 '15 at 15:17
  • Try to escape `&`. Try to print value of the parameters e.g. `req.query[params]`. – Aleksandr M Apr 09 '15 at 17:40
  • Came home, tried it with my browser and it works like a charm. This is an issue with curl, or curl within the cloud9 development environment – Alan Apr 09 '15 at 18:01

3 Answers3

2

For me, curl command worked when the url was enclosed in ""

curl "http://127.0.0.1:8080/path/resource/myname?user=alanz&password=alpacasocks"

returned both user and password in req.query

1
var data = req.query;

req.query would yield like following object

{user:"alanz", password: "alpacasocks"}

You can extract username and password by dot notation

va username = data.user;      //print "alanz"
va password = data.password;  //print "alpacasocks"

Thanks

Dineshaws
  • 2,065
  • 16
  • 26
0

Since in express' api it stands like this:

// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse

req.query.order
// => "desc"

req.query.shoe.color
// => "blue"

req.query.shoe.type
// => "converse"

I think it would be like this:

// GET /users?user[name]=myname&user[password]=mypassword

req.query.user.name
// => "myname"

req.query.user.password
// => "mypassword

Because you are querying for 2 values of the same object, and not for 2 different objects. Maybe you just want to use it as params, like in here.

PS: In any case, I wouldn't treat a password like that in any real scenario.

Community
  • 1
  • 1
Ruben Marrero
  • 1,392
  • 1
  • 10
  • 23