11

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?

bressain
  • 899
  • 9
  • 20
  • I'm interested in this as well. Did you manage to solve the problem eventually? – Para Feb 24 '13 at 15:00
  • 2
    I gave up on RestSharp. I had to use a standard HttpWebRequest to send a message that was weird like this. – bressain Feb 26 '13 at 17:39

1 Answers1

14

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

request.AddParameter("application/json", json, ParameterType.RequestBody);
Rune Vikestad
  • 4,642
  • 1
  • 25
  • 36
  • This works perfectly when the 'json' variable is a string - haven't tried throwing JObjects at it... (serviceinfo for other people finding this page) – Julian Apr 22 '15 at 08:09