简体   繁体   English

RestSharp - 数组作为查询字符串参数

[英]RestSharp - array as querystring parameter

i can't pass an array (an int array) as query string parameter to RestSharp client 我无法将数组(一个int数组)作为查询字符串参数传递给RestSharp客户端

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 我想这个问题与参数的UrlEncoding有关

in case i pass the values with "new int[] {1,2,3}" (both AddParameter and AddQueryParameter) the url is built in this way: 如果我使用“new int [] {1,2,3}”(AddParameter和AddQueryParameter)传递值,则url以这种方式构建:

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: 如果我将值作为字符串“[1,2,3]”(AddParameter和AddQueryParameter)传递,则url以这种方式构建:

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. “AddParameter”方法对逗号进行编码,但不对[]进行编码,如果是相反的话。

is there a way to change this behaviour? 有没有办法改变这种行为? does exist something like a PreExecute event where to relpace characters? 确实存在类似PreExecute事件的地方重新空格字符? or some other workaround? 或其他一些解决方法?

The way I achieved this was to use String.Join() 我实现这一点的方法是使用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()); 这行var stringOfInts = String.Join(",", new List<int>(messageIds).ConvertAll(i => i.ToString()).ToArray()); converts your int array to a list. 将int数组转换为列表。 This allows us to the use a linq expression on the list to convert each integer in the list to a string. 这允许我们在列表上使用linq表达式将列表中的每个整数转换为字符串。 Once that is done, it converts the list back to an array so you can use string.Join() on it. 完成后,它会将列表转换回数组,以便您可以使用string.Join()。 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 stringOfInts将设置为“1,2,3”,这意味着上面整个代码段的输出(作为URL)将是{_endpoint}?messageIds=1,2,3

EDIT: Credit goes to the following post: int array to string 编辑:信用转到以下帖子: int数组到字符串

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 派对有点晚了,但这也可以通过使用LINQ来实现,而我在我的案例中所做的就是

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 上面的代码生成了一个URL,它将您的整数列表作为查询字符串附加到您的请求中

Your_Service_Name?userIds=123&userIds=456

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM