如何使用RestSharp发布原始Json?

时间:2012-09-10 16:27:27

标签: .net json restsharp

我有一个端点,它接受一个带有消息元素的Json对象,然后其余的可以有不同的属性。这是一个例子:

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?

1 个答案:

答案 0 :(得分:8)

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

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