如何使用RestSharp发布Raw Json? [英] How Do I Post Raw Json Using RestSharp?

查看:298
本文介绍了如何使用RestSharp发布Raw Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个端点,该端点采用一个具有message元素的Json对象,然后其余对象可以具有不同的属性.这是一个示例:

I have an endpoint that takes a Json object that has a message element and then the rest can have different properties. Here's an example:

public void SendMessage(IDictionary<string, string> message)
{
    var client = new RestClient(MahUrl);
    var request = new RestRequest(Method.POST);
    var json = new JObject();

    foreach (var pair in message)
    {
        json.Add(pair.Key, pair.Value);
    }
    json = new JObject(new JProperty("message", json));
    // {
    //     "message":
    //     {
    //         "prop1": "val1",
    //         "foo": "bar",
    //         "batman": "robin"
    //     }
    // }

    // not quite sure here
    request.?

    // send request
}

我已经看到了很多示例,您可以对.Net对象进行序列化/反序列化,但是如您所见,json对象的属性可以是任何东西.我怎样才能使用RestSharp发布原始json?

I've seen a bunch of examples of how you can serialize/deserialize a .Net object but as you can see, the json object's properties could be anything. How can I just post raw json using RestSharp?

推荐答案

我相信您正在寻找以下代码段.

I believe the following snippet is what you're looking for.

request.AddParameter("application/json", json, ParameterType.RequestBody);

这篇关于如何使用RestSharp发布Raw Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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