I know this post is really old, but I have to reply because although BalusC's answer is marked as correct, it's not completely correct.
You have to write the query adding "[]" to foo like this:
foo[]=val1&foo[]=val2&foo[]=val3
Answer from user1922543 on Stack OverflowI know this post is really old, but I have to reply because although BalusC's answer is marked as correct, it's not completely correct.
You have to write the query adding "[]" to foo like this:
foo[]=val1&foo[]=val2&foo[]=val3
That depends on what the target server accepts. There is no definitive standard for this. See also a.o. Wikipedia: Query string:
While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field (e.g.
field1=value1&field1=value2&field2=value3).[4][5]
Generally, when the target server uses a strong typed programming language like Java (Servlet), then you can just send them as multiple parameters with the same name. The API usually offers a dedicated method to obtain multiple parameter values as an array.
foo=value1&foo=value2&foo=value3
String[] foo = request.getParameterValues("foo"); // [value1, value2, value3]
The request.getParameter("foo") will also work on it, but it'll return only the first value.
String foo = request.getParameter("foo"); // value1
And, when the target server uses a weak typed language like PHP or RoR, then you need to suffix the parameter name with braces [] in order to trigger the language to return an array of values instead of a single value.
foo[]=value1&foo[]=value2&foo[]=value3
_GET["foo"]; // [value1, value2, value3]
echo is_array($foo); // true
In case you still use foo=value1&foo=value2&foo=value3, then it'll return only the first value.
_GET["foo"]; // value1
echo is_array($foo); // false
Do note that when you send foo[]=value1&foo[]=value2&foo[]=value3 to a Java Servlet, then you can still obtain them, but you'd need to use the exact parameter name including the braces.
String[] foo = request.getParameterValues("foo[]"); // [value1, value2, value3]
How to pass array as a parameter
Array Parameter in GET request
Post an array in a HTTP request
Send array with GET Request - javascript
I'm implementing filter, sorting, pagination and search functionality via API call. For filter values i have to send array of objects in parameters.
Now I know when we want to send complex params in query we use POST method. But still wants to understand.
In my application, swagger stringify does it without any issue. I can't get the same format which is as below.
e g. This is i want in request headers (payload)
filter:[{name:"descriptive", value: "test"}]
I have tried qs package and JSON.stringify
qs.stringify(params)
or with arrayFormat but not getting the above format in payload.
Instead I'm getting this, which is not acceptable
filter[0][name]: descriptive
filter[0][value]: test
any help would be appreciated.
If you want to pass an array as parameter, you would have to add a parameter for each element. Your query string would become:
?arr[]=1&arr[]=2&arr[]=3&arr[]=4
As others have written, you can also serialize and unserialize the array.
But do you really have to send the data to the client again? It looks like you just need a way to persist the data between requests.
In this case, it is better imo to use sessions(docs). This is also more secure as otherwise the client could modify the data.
Use serialize and unserialize PHP function.
This function giving you storable (string) version of array type.
For more infomation about usage read
http://php.net/manual/en/function.serialize.php
and http://www.php.net/manual/en/function.unserialize.php
One option is using a JSON format.
http://server/url?array=["foo","bar"]
Server side
var arr = JSON.parse(req.query.array);
Or your own format
http://server/url?array=foo,bar
Server side
var arr = req.query.array.split(',');
Express exposes the query parameter as an array when it is repeated more than once in the request URL:
app.get('/', function(req, res, next) {
console.log(req.query.a)
res.send(200)
}
GET /?a=x&a=y&a=z:
// query.a is ['x', 'y', 'z']
Same applies for req.body in other methods.