Jay Chung
Jay Chung

Reputation: 109

Nodejs "querystring" module

I used built-in "querystring" module in my project.

Here is my sample code

var querystring = require('querystring');
var postData = querystring.stringify({
    travelDate: "19-Sep-2015",
    travelDate: "05-Nov-2015",
});

console.dir(postData);

But it logs 'travelDate=05-Nov-2015'.

I expected to get the result 'travelDate=19-Sep-2015&travelDate=05-Nov-2015'

Is there any method to solve the problem?

Update : I illustrate my problem more details

I know the query parameters key should be unique.But I need to fetch data from the website.I observe it's format of Form Data.

They have identical Keys("travelDate") showed below.

_eventId:showWtLblResult
mode:searchResultInter
wvm:WVMD
tripType:RT
origin:KHH
destination:NRT
travelDate:19-Sep-2015
travelDate:05-Nov-2015
adults:1
children:0
infants:0
cabinClass:ECONOMY
promoCode:
pointOfPurchase:OTHERS
flightNumberOW:
fareOW:
flightNumberRT:
fareRT:
channel:PB
bookingSource:
skyscanner_redirectid:
flexTrvlDates:

So I can't wrap my request body concisely.

I am a little stubborn that I hope to keep my code concise.

Upvotes: 0

Views: 2118

Answers (3)

long.luc
long.luc

Reputation: 1191

In order to use two identical keys for query string, here is an example:

var postData = querystring.stringify({
    travelDate: [
        "19-Sep-2015",
        "05-Nov-2015"
    ]
});

Upvotes: 1

Caleb Brinkman
Caleb Brinkman

Reputation: 2529

You're using two identical keys in the same object; the second is overwriting the first here, which is why you're seeing the results you are. You should instead use this:

var querystring = require('querystring');
var postData = querystring.stringify({
    travelDate01: "19-Sep-2015",
    travelDate02: "05-Nov-2015",
});

console.dir(postData);

Or something similar.

If, however, the querystring parameter names must be identical, you can pass them as an array

var querystring = require('querystring');
var postData = querystring.stringify({
    travelDate: [
        "19-Sep-2015",
        "05-Nov-2015"
    ]
});

Upvotes: 0

Jayram
Jayram

Reputation: 19588

query parameters key should be unique. Why do not you have something like this:

var querystring = require('querystring');
var postData = querystring.stringify({
    travelStartDate: "19-Sep-2015",
    travelEndDate: "05-Nov-2015",
});

console.dir(postData);

Which yields the result like:

'travelStartDate=19-Sep-2015&travelEndDate=05-Nov-2015'

Upvotes: 0

Related Questions