I haven't used
RestSharp
before but here's my take:
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. Here's a good article that demonstrates what I am talking about:
ASP.NET Web API, RestSharp and Model Error Messages | DotNetCurry[
^]
Second, verify that you set the
content-type
to
application/json
in Postman configuration.
third, your
Get
end-point doesn't have a return that's why you are getting
null
. You should have something like:
public IEnumerable<product> GetAll()
{
var client = new RestClient("http://localhost:4282/");
var request = new RestRequest("api/products", Method.GET) { RequestFormat = DataFormat.Json };
var response = client.Execute<List<product>>(request);
if (response.Data == null)
throw new Exception(response.ErrorMessage);
return response.Data;
}
fourth, your
Post
endpoint is void, thus it is returning
204
(No Nontent). You can return an
IHttpActionResult
something like:
public IHttpActionResult Post([FromBody] Product p)
{
var client = new RestClient("http://localhost:4282/");
var request = new RestRequest("api/products", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(p);
return Ok(client.Execute(request));
}
and finally, when designing a
REST API
with Web API, I'd suggest to give this short article a read:
ASP.NET Core and Web API: A Custom Wrapper for Managing Exceptions and Consistent Responses[
^]