简体   繁体   中英

How Do I Post Raw Json Using RestSharp?

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?

我相信以下片段是您正在寻找的。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM