RestSharp - array as querystring parameter
Asked Answered
Y

2

6

i can't pass an array (an int array) as query string parameter to RestSharp client

var client = new RestClient(baseUrl);
client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest(_endpoint, Method.GET);
            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("contenttype", "application/json; charset=utf-8");
            request.AddHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");


//i tried with
request.AddParameter("messageIds", "[1,2,3]");
or
request.AddParameter("messageIds", new int[] {1,2,3} );
or
request.AddQueryParameter("messageIds", "[1,2,3]");
or
request.AddQueryParameter("messageIds", new int[] {1,2,3} );

i suppose the problem is related to the UrlEncoding of the parameters

in case i pass the values with "new int[] {1,2,3}" (both AddParameter and AddQueryParameter) the url is built in this way:

ResponseUri = {https://demo.xxxxxxxx.com:8181/ws/messages/zippedMessages?messageIds=System.Int32[]}

in case i pass the values as a string "[1,2,3]" (both AddParameter and AddQueryParameter) the url is built in this way:

ResponseUri = {https://demo.xxxxxxxx.com:8181/ws/messages/zippedMessages?messageIds=[1%2C2%2C3]}

instead a working url should it be:

ResponseUri = {https://demo.xxxxxxxx.com:8181/ws/messages/zippedMessages?messageIds=%5B1,2,3%5D}

or at least:

ResponseUri = {https://demo.xxxxxxxx.com:8181/ws/messages/zippedMessages?messageIds=[1,2,3]}

the "AddParameter" method encodes the comma but not the [ ] , should it be the opposite.

is there a way to change this behaviour? does exist something like a PreExecute event where to relpace characters? or some other workaround?

Yuletide answered 7/4, 2017 at 7:23 Comment(6)
In the actual request it should be presented as messageIds=a&messageIds=b&messageIds=c. I've not used the RESTSharp client, but is it possible to simply add messageIds multiple times?Hankypanky
Possible duplicate of Passing list of int to Web API from RestSharp ClientHankypanky
John this produce an url like you wrote me and the API that i'm consuming is stupid enough to not accept parameters it this way. Unfortunately i can't change it.Yuletide
Have they defined the format they expect arrays to be passed in?Hankypanky
trying these formats with postman the api works: demo.xxxxxxxx.com:8181/ws/messages/… demo.xxxxxxxx.com:8181/ws/messages/…Yuletide
I looked at this and it seems that the accepted answer is right: RestSharp is hard coded to always encode the parameters. I don't see how you can pass the information you need into RestSharp. Maybe someone better than me can :) I doubt it helps, but fyi: RestSharp's source used Uri.EncodeDataString()Hankypanky
C
5

Am a little late for the party but the way this can also be achieved is by using LINQ and what I did in my case was

var request = new RestRequest("ServiceName", Method.GET);

var userIds = new List<int> { 123, 456};
     userIds.ForEach(x => request.AddParameter("userIds", x, ParameterType.GetOrPost));

The above code generates the URL which appends your integer list as a query string in your request

Your_Service_Name?userIds=123&userIds=456

hope this helps

Controversial answered 29/3, 2019 at 15:14 Comment(1)
Even later to the party. First of all, thanks for your example, it works perfectly. I just want to mention that the `ParameterType.GetOrPost' can be omitted.Windowsill
T
3

The way I achieved this was to use String.Join()

int[] messageIds = {1, 2, 3};
var request = new RestRequest(_endpoint, Method.GET);
var stringOfInts = String.Join(",", new List<int>(messageIds).ConvertAll(i => i.ToString()).ToArray());
request.AddQueryParameter("messageIds", stringOfInts);

This line var stringOfInts = String.Join(",", new List<int>(messageIds).ConvertAll(i => i.ToString()).ToArray()); converts your int array to a list. This allows us to the use a linq expression on the list to convert each integer in the list to a string. Once that is done, it converts the list back to an array so you can use string.Join() on it. The comma is a separator.

stringOfInts will be set to "1,2,3" which means the output of the entire code segment above (as a URL) will be {_endpoint}?messageIds=1,2,3

EDIT: Credit goes to the following post: int array to string

Throttle answered 31/5, 2018 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.