RestSharp JSON Parameter Posting RestSharp JSON Parameter Posting json json

RestSharp JSON Parameter Posting


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");


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.


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":"sam@company.com" ,  "password":"welcome" }