I would like to pass an array through a http header.
Would it be acceptable to name multiple params the same name, and that way I would know that they belong to an array just like in a get request query string? Example:
CurrentHeaderArray: myarray[]=value1&myarray[]=value2&myarray[]=value3
There is already a stackoverflow answer to pass it through the query string of a get request, see this hyper link.
How to pass an array within a query string?
You can pass an array as string with some delimiter char as the way csv file does. Then, in the server side code, just use some string split function to get back the array.
If the string contains the delimiter charater, escapes them.
You can pass an array as a header like this:
CurrentHeaderArray : [ "value1", "value2", "value3" ]
You can easily try this in a tool like Fiddler, using the Composer.
Related
I have some issues with passing array of parameters to query string for GET method, for example, /resource&item=1&item=2&item=3.
I have tried to pass parameters separated by commas and by &, it doesn`t work. How to configure API Gateway to do this? Can anyone help me?
Your example was using an ampersand (&) instead of a question mark (?) for separating the query string parameter from the path. I'm assuming that's just a typo.
Try passing the array using json syntax like
/resource?item=['1','2','3']
have you tried this way :
/resource&item[]=1&item[]=2&item[]=3
The way you used would erase the first data by the last data in the url.
What we are doing in our company is to pass data separated by ,. On Backend we explode the parameter and make it array again. I am not sure if there is more better way to do it or not. Let me know if you find any.
like ?items=1,2,3,4
And we get explode items with , through extra code
and get result as [1,2,3,4]
I have a complex odata query that I am passing through Angular using OdataAngularResrouce library. As we all know that odata queries are case sensitive and obviously I dont know how the data is being stored in the database. Here is the predicates that I used to build the query.
var predicFirstName = new $odata.Predicate(new $odata.Func("startswith", "FirstName", new $odata.Func("tolower", $scope.searchObject.searchString)), true);
var predicLastName = new $odata.Predicate(new $odata.Func("startswith", "LastName", new $odata.Func("tolower", $scope.searchObject.searchString)));
**OData URI:**
https://localhost/app/TylerIdentityUserAdministrationService/Users/$count/?$filter=((startswith(FirstName,tolower(Fahad)))%20or%20startswith(LastName,tolower(Fahad)))
As you can see, I want to put a function to check only the given string with startswith. I have seen several posts where the solution is to put the tolower(). However, when I put it the way mentioned above its not returning any data. Can anybody help here?
Thanks
-Fahad
Both the properties and the literal strings in the $filter need to be converted to lowercase. And since the literal strings originate on the client, you can optimize by converting them to lowercase before you send the OData request.
$filter=startswith(tolower(FirstName),'fahad') or startswith(tolower(LastName),'fahad')
Note that the literal strings must be surrounded with single quotes in the filter expression.
I know that the standard method for passing named parameters in a get request, is:
?paramName=value&anotherParam=anotherValue...
In my case, I want to pass an array of parameters
However, I want to pass multiple parameters with the same meaning - an array.
In js that would be
var users = ['bob', 'sam', 'bill'];
and I want to pass the users array via get.
What would be the way to accomplish this?
You will need to serialize them into some form of string that can be re-parsed into an array.
For, example, you could use...
?param[]=a¶m[]=b¶m[]=c
...or something like...
?params=[a][b][b].
my web-service return me an array composed of two arrays, here how it looks like on the web-service:
$finalArray=array($array1,$array2);
sendResponse(200,json_encode($finalArray));
each array contains a list of simple values :
array1{gazole,sp98,GPL,gazole+}
array2{TOTAL,SHELL,ESSO}
in my iPhone side, i would like to parse this and to put the content of each array on an NSArray, i used to do that :
//parse the response
NSArray *array=[[request responseString]JSONValue];
i'm little confused for the rest, please help, thx in advance :)
Use the objectAtIndex method of NSArray.
I have many parameters to pass to the server using JAX-RS.
Is there a way to pass or AarryList with the URL?
You have a few options here.
Option 1: A query parameter with multiple values
You can supply multiple simple values for a single query parameter. For example, your query string might look like:
PUT /path/to/my/resource?param1=value1¶m1=value2¶m1=value3
Here the request parameter param1 has three values, and the container will give you access to all three values as an array (See Query string structure).
Option 2: Supply complex data in the PUT body
If you need to submit complex data in a PUT request, this is typically done by supplying that content in the request body. Of course, this payload can be xml (and bound via JAXB).
Remember the point of the URI is to identify a resource (RFC 3986, 3.4), and if this array of values is data that is needed to identify a resource then the URI is a good place for this. If on the other hand this array of data forms part of the new representation that is being submitted in this PUT request, then it belongs in the request body.
Having said that, unless you really do just need an array of simple values, I'd recommend choosing the Option 2. I can't think of a good reason to use URL-encoded XML in the URL, but I'd be interested to hear more about exactly what this data is.
We can get the Query parameters and corresponding values as a Map,
#GET
#Produces(MediaType.APPLICATION_JSON)
public void test(#Context UriInfo ui) {
MultivaluedMap<String, String> map = ui.getQueryParameters();
String name = map.getFirst("name");
String age = map.getFirst("age");
System.out.println(name);
System.out.println(age);
}