0

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.

2
  • 2
    An object can't have two identical keys Commented Sep 7, 2015 at 6:14
  • In fact, in ES5 strict mode, duplicate keys on an object is actually a syntaxError. In ES6, it is allowed, but the second will overwrite the first so the object will only have the second key/value. Object keys must be unique. Commented Sep 7, 2015 at 6:18

3 Answers 3

1

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"
    ]
});
Sign up to request clarification or add additional context in comments.

Comments

0

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"
    ]
});

1 Comment

Of course. :) if any of the answers have helped, you should consider accepting and/or upvoting them.
0

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'

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.