RestSharp JSON Parameter Posting

250,957

Solution 1

You don't have to serialize the body yourself. Just do

request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new { A = "foo", B = "bar" }); // Anonymous type object is converted to Json body

If you just want POST params instead (which would still map to your model and is a lot more efficient since there's no serialization to JSON) do this:

request.AddParameter("A", "foo");
request.AddParameter("B", "bar");

Solution 2

In the current version of RestSharp (105.2.3.0) you can add a JSON object to the request body with:

request.AddJsonBody(new { A = "foo", B = "bar" });

This method sets content type to application/json and serializes the object to a JSON string.

Solution 3

This is what worked for me, for my case it was a post for login request :

var client = new RestClient("http://www.example.com/1/2");
var request = new RestRequest();

request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", body , ParameterType.RequestBody);

var response = client.Execute(request);
var content = response.Content; // raw content as string  

body :

{
  "userId":"[email protected]" ,
  "password":"welcome" 
}

Solution 4

Hope this will help someone. It worked for me -

RestClient client = new RestClient("http://www.example.com/");
RestRequest request = new RestRequest("login", Method.POST);
request.AddHeader("Accept", "application/json");
var body = new
{
    Host = "host_environment",
    Username = "UserID",
    Password = "Password"
};
request.AddJsonBody(body);

var response = client.Execute(request).Content;
Share:
250,957

Related videos on Youtube

Wesley Tansey
Author by

Wesley Tansey

Co-founder at Curvio and Machine Learning PhD Student in the Neural Networks Research Group at UT Austin

Updated on January 17, 2021

Comments

  • Wesley Tansey
    Wesley Tansey over 3 years

    I am trying to make a very basic REST call to my MVC 3 API and the parameters I pass in are not binding to the action method.

    Client

    var request = new RestRequest(Method.POST);
    
    request.Resource = "Api/Score";
    request.RequestFormat = DataFormat.Json;
    
    request.AddBody(request.JsonSerializer.Serialize(new { A = "foo", B = "bar" }));
    
    RestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);
    

    Server

    public class ScoreInputModel
    {
       public string A { get; set; }
       public string B { get; set; }
    }
    
    // Api/Score
    public JsonResult Score(ScoreInputModel input)
    {
       // input.A and input.B are empty when called with RestSharp
    }
    

    Am I missing something here?

  • Wesley Tansey
    Wesley Tansey almost 13 years
    Both. The second approach is much faster though.
  • John Sheehan
    John Sheehan almost 13 years
    You can do AddObject(new { A = "foo", B = "bar" }) too which takes the object properties and converts them into parameters
  • Ivan G.
    Ivan G. about 12 years
    what if element name contains dash (-)?
  • John Sheehan
    John Sheehan about 12 years
    Use [SerializeAs] for XML, or whatever JSON.NET uses to accomplish the same thing.
  • Kyle Patterson
    Kyle Patterson about 11 years
    For those that want to jsonize themselves: request.AddParameter("text/json", body, ParameterType.RequestBody);
  • John Sheehan
    John Sheehan about 11 years
    @KylePatterson you can also implement your own ISerializer and set RestClient.JsonSerializer to use it.
  • Scott
    Scott almost 11 years
    @JohnSheehan perhaps this is a recent change, but the JsonSerializer is set on the RestRequest, not the RestClient. RestClient has a Deserializer, but it's set using AddHandler.
  • Benjamin
    Benjamin almost 10 years
    There's one obsolete parantheses after the request.AddBody(new ... in your first example.
  • Gaui
    Gaui over 8 years
    AddHeader and AddBody didn't work, but AddParameter method @KylePatterson suggested, works. Weird!
  • mac10688
    mac10688 over 8 years
    For me, using the AddParameters caused an internal server error. I had to use the first method.
  • OPV
    OPV almost 7 years
    How to attach file to this request?
  • OPV
    OPV almost 7 years
    How to attach file to this request?
  • mdegges
    mdegges over 6 years
    how do you name the object? eg. if you need to send "details" : { "extra" : "stuff" } ?
  • Chris Morgan
    Chris Morgan about 6 years
    @OPV You can add a file to the request like this: request.AddFile(pathToTheFile);
  • Chris Morgan
    Chris Morgan about 6 years
    @mdegges If you are using an anonymous class as the body to have the JSON look like your example setup the RestSharp Request like this: var client = new RestSharp.RestClient("http://your.api.com"); var request = new RestSharp.RestRequest("do-something", Method.POST); var body = new {details = new {extras = "stuff"}}; request.AddJsonBody(body); var response = client.Execute(request);
  • Kynao
    Kynao almost 6 years
    How do you insert the body into your c# code ? as string body = "{ "userId":"[email protected]" , "password":"welcome" }"; does not work.
  • Vitaly Ascheulov
    Vitaly Ascheulov almost 5 years
    You should use "" instead of " string body = @" { ""userid"", ... "
  • Bimal Poudel
    Bimal Poudel over 4 years
    Looks like request.AddHeader("Accept", "application/json"); is the correct answer.
  • Andrew
    Andrew over 4 years
    deprecated/doesnt work for json (it's defaulting to xml??), use AddJsonBody()
  • Lei Yang
    Lei Yang over 2 years
    what's the syntax if json(dict) contains an array?
  • blac040
    blac040 about 2 years
    If I have " var myJSON = "{ JSON Content }" , then what changes I have to make in AddParameters ?