Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can RestSharp send a List<string> in a POST request?

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

My object:

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

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);

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

like image 237
adrianbanks Avatar asked Sep 05 '12 15:09

adrianbanks


1 Answers

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));
like image 86
Josh Kodroff Avatar answered Oct 12 '22 21:10

Josh Kodroff