115

I'm trying to use RestSharp to consume a web service. So far everything's gone very well (cheers to John Sheehan and all contributors!) but I've run into a snag. Say I want to insert XML into the body of my RestRequest in its already serialized form (i.e., as a string). Is there an easy way to do this? It appears the .AddBody() function conducts serialization behinds the scenes, so my string is being turned into <String />.

Any help is greatly appreciated!

EDIT: A sample of my current code was requested. See below --

private T ExecuteRequest<T>(string resource,
                            RestSharp.Method httpMethod,
                            IEnumerable<Parameter> parameters = null,
                            string body = null) where T : new()
{
    RestClient client = new RestClient(this.BaseURL);
    RestRequest req = new RestRequest(resource, httpMethod);

    // Add all parameters (and body, if applicable) to the request
    req.AddParameter("api_key", this.APIKey);
    if (parameters != null)
    {
        foreach (Parameter p in parameters) req.AddParameter(p);
    }

    if (!string.IsNullOrEmpty(body)) req.AddBody(body); // <-- ISSUE HERE

    RestResponse<T> resp = client.Execute<T>(req);
    return resp.Data;
}
AakashM
  • 62,551
  • 17
  • 151
  • 186
Matt G.
  • 1,742
  • 2
  • 11
  • 16
  • What does your current code look like? And where in it do you have the problem? – Oded Feb 23 '11 at 19:07
  • 2
    sorry, didn't see this until now. you probably want AddParameter() for that. if that's not what you want, post to the google group with an example of the body with params + xml that you're trying to achieve. http://groups.google.com/group/restsharp – John Sheehan Mar 02 '11 at 07:35

2 Answers2

239

Here is how to add plain xml string to the request body:

req.AddParameter("text/xml", body, ParameterType.RequestBody);

dmitreyg
  • 2,615
  • 1
  • 19
  • 20
  • 34
    +1 Likewise, to add plain JSON, it's req.AddParameter("text/json", body, ParameterType.RequestBody); – Paul Prewett Mar 01 '12 at 02:07
  • 53
    Actually, for Json it should be (at least for Rails) : `req.AddParameter("application/json", body, ParameterType.RequestBody);` Thanks to Jean Hominal for the tip [here](http://stackoverflow.com/questions/14141174/c-sharp-adding-double-quotes-to-start-and-end-of-json-string-results-in-server-d/14141580#14141580) – MrWater Jan 03 '13 at 15:26
  • 1
    How would I go abouts setting this to just a HTML encoded string? I.e. a large get val=2&val2=3 etc. – mike james Oct 22 '13 at 15:55
  • 4
    I would think req.AddParameter("application/x-www-form-urlencoded", body, ParameterType.RequestBody); – Brian Rice Nov 04 '13 at 06:37
  • Is there possible to pipe stream to body stream? – hellboy Jun 22 '15 at 13:43
  • 1
    This comes with a size restriction of 64 KB, which results in return status codes of 0 where the size of the body is greater than 64 KB. – StampyTurtle Mar 07 '16 at 22:15
  • 3
    I'm using the latest version of RestSharp, and this method signature is not available. – jmrah Dec 02 '16 at 18:31
7

To Add to @dmitreyg's answer and per @jrahhali's comment to his answer, in the current version, as of the time this is posted it is v105.2.3, the syntax is as follows:

request.Parameters.Add(new Parameter() { 
    ContentType = "application/json", 
    Name = "JSONPAYLOAD", // not required 
    Type = ParameterType.RequestBody, 
    Value = jsonBody
});

request.Parameters.Add(new Parameter() { 
    ContentType = "text/xml", 
    Name = "XMLPAYLOAD", // not required 
    Type = ParameterType.RequestBody, 
    Value = xmlBody
});
interesting-name-here
  • 1,851
  • 1
  • 20
  • 33
  • 1
    I tried this, but what ever I set in the Name parameter is actually set as content-Type. So for both ContentType and Name, I used "application/json". – Thangadurai Sep 26 '17 at 06:44
  • I tried this and got a null reference exception from within rest#. Found a solution at https://stackoverflow.com/a/44281853/109736 – JasonCoder Jul 26 '19 at 21:19
  • @JasonCoder thanks for the comment. Was it the same version as well, v105.2.3? I've had mixed results with later versions is the reason I ask. It's now on 106 which may not operate the same. – interesting-name-here Aug 07 '19 at 20:27
  • @GibralterTop my results were with 106.6.9 – JasonCoder Aug 08 '19 at 18:41