Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restsharp- Method.POST is not working

I have tried all possible scenarios as per 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 above code Parameter posted in Body is always null.

When, I made following call, Parameter posted in Body is having value but 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 will be mostly welcomed!

like image 806
Gaurav Arora Avatar asked Dec 25 '14 19:12

Gaurav Arora


People also ask

Is restsharp a REST client?

As I've understand after googling, RestSharp is a REST Http Client. This means that you build Web APIs separately and use RestSharp within your .NET code to consume them. Here's a good article that demonstrates what I am talking about: ASP.NET Web API, RestSharp and Model Error Messages | DotNetCurry [ ^]

Why can't I call the same endpoint in restsharp?

First off, your code doesn't make sense. You're creating a Web API endpoint and your are calling/consuming the same endpoint within the endpoint implementation. As I've understand after googling, RestSharp is a REST Http Client. This means that you build Web APIs separately and use RestSharp within your .NET code to consume them.

What is the difference between get and POST request with Postman?

When l make a get request with Postman it returns 200 and in the body it gives me null instead of json and when l make a post request with Postman it returns a 204 (no content). All suggestions are welcomed. Thanks. Please Sign up or sign in to vote.

What is the difference between executegetasync and executepostasync in restsharp?

Similarly to the ExecuteGetAsync method, the ExecutePostAsync method executes only the POST request, and we don’t have to provide the HTTP method parameter while creating the RestRequest object. To explain how to handle the PUT request using RestSharp, let’s create an action that updates the user and job details:


1 Answers

I found the solution. Answering my own question so, that folks who are facing similar issue can find the solution.

Just need to do the request as:

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

Following 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, resolved my issue.

like image 55
Gaurav Arora Avatar answered Sep 28 '22 15:09

Gaurav Arora