RestSharp发送字典 [英] RestSharp send Dictionary

查看:41
本文介绍了RestSharp发送字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了如何从响应中反序列化字典,但是如何发送一个字典?

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

推荐答案

呃……是其他事情搞砸了我的案子.正如@Chase 所说,这很简单:

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.

(*) 确认它使用 Fiddler 或 RestSharp 的 简单服务器

(*) confirmed that it sent expected JSON with an echo service like Fiddler, or RestSharp's SimpleServer

这篇关于RestSharp发送字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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