Hi
How to pass Dynamic Parameters in body of RestSharp in asp.net c#
Thanks
Hi
How to pass Dynamic Parameters in body of RestSharp in asp.net c#
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Uday DodiyaRank #239 · 10.3K reputationPosted Jan 8, 2023, 11:03 PM EST
To pass dynamic parameters in the body of a RestSharp request in ASP.NET C#, you can use the
AddParametermethod of theRestRequestclass and set theTypeproperty toParameterType.RequestBody.Here's an example of how you could do this:
var client = new RestClient("http://example.com"); var request = new RestRequest("resource/{id}", Method.POST); // Add the dynamic parameters to the request body request.AddParameter("param1", "value1", ParameterType.RequestBody); request.AddParameter("param2", "value2", ParameterType.RequestBody); // Execute the request IRestResponse response = client.Execute(request);You can also add multiple parameters to the request body by using the
AddJsonBodyorAddXmlBodymethods of theRestRequestclass, depending on the format of the data you want to send.Pasang TamangRank #449 · 5.6K reputationPosted Jan 8, 2023, 11:07 PM EST
Hi
There are various ways. You can use dynamic JSON object or use class object that have matching properties with required JSON format and serialize that class object values using Newtonsoft.Json. Below is the example of using dynamic JSON object.
var client = new RestClient("http://endpoint-baseurl.com"); var request = new RestRequest("/your-api-endpoint}", Method.POST); // add dynamic parameters to the request body request.AddJsonBody(new { param1 = "value1", param2 = "value2" }); // execute the request var response = client.Execute(request);Regards,
Pasang