How Do I Post Raw Json Using RestSharp?

戏子无情 提交于 2019-12-10 12:54:48

问题


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
}

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?


回答1:


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

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


来源:https://stackoverflow.com/questions/12355777/how-do-i-post-raw-json-using-restsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!