Send an Array with an HTTP Get
Asked Answered
G

3

130

How can i send an Array with a HTTP Get request?

I'm Using GWT client to send the request.

Guanabana answered 17/6, 2010 at 11:37 Comment(1)
really, HTML is better at illuminating this. <input name="user[name]" /> <input name="user[email]" /> inside of a <form> container knows exactly how to submit by default in all browsers. inputs will become `user[name]=SOMETEXT&user[email][email protected] --- it is the host application which is required to handle decoding back into an arrayKeitt
A
185

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
$foo = $_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.

$foo = $_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]
Another answered 17/6, 2010 at 11:40 Comment(8)
when server uses nodeJs, you can simply pass array as foo=[value1,value2,value3] .When you obtain this inside express route with req.query, you will get {foo:'[value1,value2,value3]'}. You just need to parse it & use this array in your codeTew
what nonsense, language typing has nothing to do with itPickings
This is indeed unspecified. That's exactly why the answer says "Generally". In strong typed languages the bracket suffixes [] in request parameter names are namely not interpreted the same way as in weak typed languages. It was originally introduced as an internal work around for weak typed languages in order to be able to create the intended variable type. PHP was the first do to so.Another
This doesn’t generalise. The brackets is a Ruby on Rails marshalling convention to encode nested structure into an otherwise flat sequence. Zope (Python framework) used a different scheme for the same use case. These schemes have mostly been obsoleted by AJAX and JSON. The idea that this should be generalised to type systems is very far fetched, to be honest.Clump
Put differently: the issue is that a query string carries no type information, not that the language parsing the query string is weakly typed. PHP and Ruby do not require the additional brackets to accept multiple values for the same name.Clump
I'm not saying that the query string carries type information, just that the brackets trigger type information in some server side languages. This happens in e.g. PHP but not in e.g. Java because it has its a separate way to obtain them as an array. If you leave out the brackets, PHP doesn't return an array anymore.Another
this approach: foo[]=value1&foo[]=value2&foo[]=value3 can send an array with indices: foo[1950]=value1&foo[201]=value2&foo[123]=value3 and etc.Rogozen
As answered, "That depends on what the target server accepts. There is no definitive standard for this".Another
K
221

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
Koski answered 21/12, 2012 at 23:38 Comment(3)
Glad you replied to this old post! It definitely helped out.Colostrum
@BalusC incorrect, this works perfectly fine in C/C++/C# (and can work in java). It is only your usage of Java which causes identical names to be interpreted as such. Your answer is incorrect usage of URI formatting howeverKeitt
@GaretClaborn not sure what you are trying to say, but this answer works for mePulliam
A
185

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
$foo = $_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.

$foo = $_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]
Another answered 17/6, 2010 at 11:40 Comment(8)
when server uses nodeJs, you can simply pass array as foo=[value1,value2,value3] .When you obtain this inside express route with req.query, you will get {foo:'[value1,value2,value3]'}. You just need to parse it & use this array in your codeTew
what nonsense, language typing has nothing to do with itPickings
This is indeed unspecified. That's exactly why the answer says "Generally". In strong typed languages the bracket suffixes [] in request parameter names are namely not interpreted the same way as in weak typed languages. It was originally introduced as an internal work around for weak typed languages in order to be able to create the intended variable type. PHP was the first do to so.Another
This doesn’t generalise. The brackets is a Ruby on Rails marshalling convention to encode nested structure into an otherwise flat sequence. Zope (Python framework) used a different scheme for the same use case. These schemes have mostly been obsoleted by AJAX and JSON. The idea that this should be generalised to type systems is very far fetched, to be honest.Clump
Put differently: the issue is that a query string carries no type information, not that the language parsing the query string is weakly typed. PHP and Ruby do not require the additional brackets to accept multiple values for the same name.Clump
I'm not saying that the query string carries type information, just that the brackets trigger type information in some server side languages. This happens in e.g. PHP but not in e.g. Java because it has its a separate way to obtain them as an array. If you leave out the brackets, PHP doesn't return an array anymore.Another
this approach: foo[]=value1&foo[]=value2&foo[]=value3 can send an array with indices: foo[1950]=value1&foo[201]=value2&foo[123]=value3 and etc.Rogozen
As answered, "That depends on what the target server accepts. There is no definitive standard for this".Another
E
0

Go to .get() end point - via HTTP GET request. And make there a request. Put data in some render as a properties and render them in some component.

Electrochemistry answered 17/8, 2023 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.