Reputation: 25004
I've seen how to deserialize a dictionary from the response, but how do you send one?
var d = new Dictionary<string, object> {
{ "foo", "bar" },
{ "bar", 12345 },
{ "jello", new { qux = "fuum", lorem = "ipsum" } }
};
var r = new RestRequest(url, method);
r.AddBody(d); // <-- how?
var response = new RestClient(baseurl).Execute(r);
Upvotes: 2
Views: 4364
Reputation: 25004
Ugh...it was something else screwing up my case. As @Chase said, it's pretty simple:
var c = new RestClient(baseurl);
var r = new RestRequest(url, Method.POST); // <-- must specify a Method that has a body
// shorthand
r.AddJsonBody(dictionary);
// longhand
r.RequestFormat = DataFormat.Json;
r.AddBody(d);
var response = c.Execute(r); // <-- confirmed*
Didn't need to wrap the dictionary as another object.
(*) confirmed that it sent expected JSON with an echo service like Fiddler, or RestSharp's SimpleServer
Upvotes: 4
Reputation: 2629
Try to do it something like this, it's my example of simple post with some stuff for you, I prefer use RestSharp in this style because it's much cleaner then other variants of using it:
var myDict = new Dictionary<string, object> {
{ "foo", "bar" },
{ "bar", 12345 },
{ "jello", new { qux = "fuum", lorem = "ipsum" } }
};
var client = new RestClient("domain name, for example http://localhost:12345");
var request = new RestRequest("part of url, for example /Home/Index", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new { dict = myDict }); // <-- your possible answer
client.Execute(request);
And for this example endpoint should have dict
parameter in declaration.
Upvotes: 1