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?

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? Commented Apr 7, 2017 at 7:29
  • 2
    Possible duplicate of Passing list of int to Web API from RestSharp Client Commented Apr 7, 2017 at 7:35
  • 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. Commented Apr 7, 2017 at 9:25
  • Have they defined the format they expect arrays to be passed in? Commented Apr 7, 2017 at 9:26
  • trying these formats with postman the api works: demo.xxxxxxxx.com:8181/ws/messages/… demo.xxxxxxxx.com:8181/ws/messages/… Commented Apr 7, 2017 at 9:28

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.