I have an application with nodejs express who makes request but when I pass an array I am:
Example: /foo?id=1&id=3&id=5
How to remove '[]' ?
var requestQueryParams = {id: [1,3,5]}
var options = {
url: 'www.test.com',
headers: {'content-type': 'application/json', 'accept': 'application/json'},
qs: requestQueryParams || {}
};
request.get(options), function(){...}
result: www.test.com?id[0]=1&id[1]=3&id[2]=5
Request.js = https://www.npmjs.org/package/request
Qs.js = https://www.npmjs.org/package/qs
Qs.stringify({ a: ['b', 'c', 'd'] });
// 'a[0]=b&a[1]=c&a[2]=d'
What do you want instead? If you want www.test.com?id0=1&id1=3&id2=5
, then you need to give it a params object like this:
var requestQueryParams = { id0: 1, id1: 3, id2: 5 }
If you already have an object that looks like { id: [1,2,3] }
, then you need to convert that object into one like the above. You can do that in a for
loop easily enough:
var requestQueryParams = { id: [1,3,5] },
newRequestQueryParams = {};
for(var i = 0; i < requestQueryParams.id.length; i++) {
var paramName = "id" + i, // "id0", "id1", etc.
id = requestQueryParams.id[i];
newRequestQueryParams[paramName] = id;
}
console.log(newRequestQueryParams);
// => { id0: 1, id1: 3, id2: 5 }
Update: If you want a query string like id=1&id=3&id=5
(although this would be very strange, as I mention in my comment below), you can also do it in a for
loop, as above, or you could do something like this:
var requestQueryParams = { id: [1,3,5] },
queryStringParts = [], // an array this time
queryString;
for(var i = 0; i < requestQueryParams.id.length; i++) {
var param = "id=" + parseInt( requestQueryParams.id[i] );
queryStringParts.push(param);
}
// queryStringParts is now [ "id=1", "id=3", "id=5" ]
queryString = queryStringParts.join("&")
console.log(queryString);
// => "id=1&id=3&id=5"
I used parseInt
inside the for
loop because I'm assuming the IDs are coming from an untrusted source (e.g. a user) and, since you're building a string manually instead of using a library that will encode the data for you, you want to prevent a malicious user from injecting arbitrary strings into your request. You could also use encodeURIComponent
, but it's overkill if IDs should always be numbers.
My Solution is override (request.Request.prototype.qs)
var qs = require('qs'),
request = require('request'),
url = require('url');
var stringify;
var toString = Object.prototype.toString;
var isArray = Array.isArray || function (arr) {
return toString.call(arr) === '[object Array]';
};
var objectKeys = Object.keys || function (obj) {
var ret = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
ret.push(key);
}
}
return ret;
};
var stringifyString = function (str, prefix) {
if (!prefix) throw new TypeError('stringify expects an object');
return prefix + '=' + encodeURIComponent(str);
};
var stringifyArray = function (arr, prefix) {
var ret = [];
if (!prefix) throw new TypeError('stringify expects an object');
for (var i = 0; i < arr.length; i++) {
ret.push(stringify(arr[i], prefix));
}
return ret.join('&');
};
function stringifyObject(obj, prefix) {
var ret = [];
var keys = objectKeys(obj);
var key;
for (var i = 0, len = keys.length; i < len; ++i) {
key = keys[i];
if ('' === key) {
continue;
}
if (null === obj[key]) {
ret.push(encodeURIComponent(key) + '=');
} else {
ret.push(stringify(obj[key], prefix ? prefix + '[' + encodeURIComponent(key) + ']' : encodeURIComponent(key)));
}
}
return ret.join('&');
}
stringify = function (obj, prefix) {
if (isArray(obj)) {
return stringifyArray(obj, prefix);
} else if ('[object Object]' === toString.call(obj)) {
return stringifyObject(obj, prefix);
} else if ('string' === typeof obj) {
return stringifyString(obj, prefix);
} else {
return prefix + '=' + encodeURIComponent(String(obj));
}
};
And override prototype.qs :
request.Request.prototype.qs = function (q, clobber) {
var base;
if (!clobber && this.uri.query) {
base = qs.parse(this.uri.query)
}
else {
base = {}
}
for (var i in q) {
base[i] = q[i]
}
if (stringify(base) === '') {
return this
}
this.uri = url.parse(this.uri.href.split('?')[0] + '?' + stringify(base));
this.url = this.uri;
this.path = this.uri.path;
return this;
};