Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js ignores all querystring parameters except the first one

I can't persuade Node.js to see the second parameter from my URL:

http://localhost:8888/cities?tag=123&date=2013-03-30T10:00:28.000%2B02:00

I start the Node.js server with the following command:

node --debug ./router_rest.js

I have the following code in my "router_rest.js" file:

require("http").createServer(function (req, res) {
  //  For PUT/POST methods, wait until the
  //  complete request body has been read.
  if (req.method==="POST" || req.method==="PUT") {
    var body = "";
    req.on("data", function(data){
      body += data;
    })

    req.on("end", function(){
      return routeCall(req, res, body);
    })

  } else {
    return routeCall(req, res, "");
  }
}).listen(8888);

I make the following call from the command line:

curl http://localhost:8888/cities?tag=123&date=2013-03-30T10:00:28.000%2B02:00

When I debug and examine the 'req' parameter (in the anonymous function above), the url property appears as:

/cities?tag=123

I was expecting the url reported to be:

/cities?tag=123&date=2013-03-30T10:00:28.000%2B02:00

Even when I switch parameters, node still only sees the first one.

Why has the last parameter (ie. date) been truncated?

like image 275
gavdotnet Avatar asked Jul 05 '13 10:07

gavdotnet


People also ask

Does the order of query params matter?

The order of parameters matters in query strings. In the following example, the query strings are identical except that the parameters are in a different order.

What can I use instead of Querystring Stringify?

The querystring. encode() function is an alias for querystring. stringify() .

How do I pass Querystring?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.

How do I clear URLSearchParams?

delete() The delete() method of the URLSearchParams interface deletes the given search parameter and all its associated values, from the list of all search parameters.


1 Answers

Node is fine.

The first ampersand in your curl command is causing the shell to run curl in the background. Add quotes around the URL and it will work.

curl "http://localhost:8888/cities?tag=123&date=2013-03-30T10:00:28.000%2B02:00"
like image 97
JasonSmith Avatar answered Sep 28 '22 11:09

JasonSmith