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!
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 [ ^]
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With