Restsharp-Method.POST not working

I have tried every possible scenario according to RestSharp.org documentation but no luck!

I have ASP.Net Web API, here is the POST resource

[Route("/api/saveperson/{name}/{fathername}")]    
public void Post([FromBody]CustomObject customObject, string name, string fatherName)
{
      //customObject is null
}

      

RestSharp request:

public void SomeAPIRequest()
{
 var baseUrl = "someurl from config";
var client = new RestClient(baseUrl);
var request = new RestRequest("/api/saverperson/{name}/{fathername}",Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(myObject);  //This object is perfectly serialized in json
request.AddParameter("name","Gaurav",ParameterType.UrlSegment);
request.AddParameter("fathername","Lt. Sh. Ramkrishan",ParameterType.UrlSegment);

var response= client.Execute(request);

}

      

With the above code, the Parameter sent to Body is always zero.

When I made the next call, the parameter sent to Body has a value and the others are null

public void SomeAPIRequest()
{
 var baseUrl = "someurl from config";
var client = new RestClient(baseUrl);
var request = new RestRequest("/api/saverperson/{name}/{fathername}",Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(myObject);  //This object is perfectly serialized in json
request.AddUrlSegment("name","Gaurav");
request.AddUrlSegment("fathername","Lt. Sh. Ramkrishan");

var response= client.Execute(request);

}

      

Any help would be mostly appreciated!

+3


source to share


1 answer


I found a solution. Answering my own question in a way that people who are facing a similar problem can find a solution.

Just execute the query like:

request.AddParameter("Application/Json", myObject, ParameterType.RequestBody);

      



Below is the complete snippet:

public void SomeAPIRequest()
{
   var baseUrl = "someurl from config";
   var client = new RestClient(baseUrl);
   var request = new RestRequest("/api/saverperson/{name}/{fathername}",Method.POST);
   request.RequestFormat = DataFormat.Json;
   request.AddParameter("Application/Json", myObject, ParameterType.RequestBody);
   request.AddUrlSegment("name","Gaurav");
   request.AddUrlSegment("fathername","Lt. Sh. Ramkrishan");

   var response= client.Execute(request);

}

      

Above code, solved my problem.

+7


source







All Articles