RestSharp JSON Parameter Posting
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;
Related videos on Youtube
Wesley Tansey
Co-founder at Curvio and Machine Learning PhD Student in the Neural Networks Research Group at UT Austin
Updated on January 17, 2021Comments
-
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 almost 13 yearsBoth. The second approach is much faster though.
-
John Sheehan almost 13 yearsYou can do
AddObject(new { A = "foo", B = "bar" })
too which takes the object properties and converts them into parameters -
Ivan G. about 12 yearswhat if element name contains dash (-)?
-
John Sheehan about 12 yearsUse [SerializeAs] for XML, or whatever JSON.NET uses to accomplish the same thing.
-
Kyle Patterson about 11 yearsFor those that want to jsonize themselves:
request.AddParameter("text/json", body, ParameterType.RequestBody);
-
John Sheehan about 11 years@KylePatterson you can also implement your own ISerializer and set RestClient.JsonSerializer to use it.
-
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 almost 10 yearsThere's one obsolete parantheses after the
request.AddBody(new ...
in your first example. -
Gaui over 8 yearsAddHeader and AddBody didn't work, but AddParameter method @KylePatterson suggested, works. Weird!
-
mac10688 over 8 yearsFor me, using the AddParameters caused an internal server error. I had to use the first method.
-
OPV almost 7 yearsHow to attach file to this request?
-
OPV almost 7 yearsHow to attach file to this request?
-
mdegges over 6 yearshow do you name the object? eg. if you need to send "details" : { "extra" : "stuff" } ?
-
Chris Morgan about 6 years@OPV You can add a file to the request like this: request.AddFile(pathToTheFile);
-
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 almost 6 yearsHow do you insert the body into your c# code ? as string body = "{ "userId":"[email protected]" , "password":"welcome" }"; does not work.
-
Vitaly Ascheulov almost 5 yearsYou should use "" instead of " string body = @" { ""userid"", ... "
-
Bimal Poudel over 4 yearsLooks like
request.AddHeader("Accept", "application/json");
is the correct answer. -
Andrew over 4 yearsdeprecated/doesnt work for json (it's defaulting to xml??), use AddJsonBody()
-
Lei Yang over 2 yearswhat's the syntax if json(dict) contains an array?
-
blac040 about 2 yearsIf I have " var myJSON = "{ JSON Content }" , then what changes I have to make in AddParameters ?