RestSharp可以发送List< string>在POST请求中? [英] Can RestSharp send a List<string> in a POST request?

查看:138
本文介绍了RestSharp可以发送List< string>在POST请求中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 RestSharp 与我拥有的宁静服务一起使用.一切似乎都正常,除非通过POST传递的对象包含一个列表(在这种情况下,是string的列表).

I am trying to get RestSharp to work with a restful service that I have. Everything seems to be working fine, except when my object being passed via POST contains a list (in this particular case a list of string).

我的对象

public class TestObj
{
    public string Name{get;set;}
    public List<string> Children{get;set;}
}

将其发送到服务器时,Children属性将作为包含内容System.Collections.Generic.List`1[System.String]的字符串发送.

When this gets sent to the server the Children property gets sent as a string with the contents System.Collections.Generic.List`1[System.String].

这是我发送对象的方式:

This is how I am sending the object:

var client = new RestClient();
var request = new RestRequest("http://localhost", Method.PUT);

var test = new TestObj {Name = "Fred", Children = new List<string> {"Arthur", "Betty"}};
request.AddObject(test);
client.Execute<TestObj>(request);

我做错什么了吗,或者这是RestSharp中的错误吗? (如果有所作为,我使用的是JSON,而不是XML.)

Am I doing something wrong, or is this a bug in RestSharp? (If it makes a difference, I am using JSON, not XML.)

推荐答案

这取决于您要连接的服务器,但是如果您要连接的是ASP.NET Web API控制器(可能还有其他服务器端技术),如果您将一个集合中的每个项目循环添加,它将起作用:

It depends on what server you're hitting, but if you're hitting an ASP.NET Web API controller (and probably other server-side technologies), it'll work if you add each item in the collection in a loop:

foreach (var child in test.Children) 
    request.AddParameter("children", x));

这篇关于RestSharp可以发送List&lt; string&gt;在POST请求中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆