Click here to Skip to main content
15,887,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning how to use RestSharp to make my web api calls. 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.

What I have tried:

Here is my product class with the set and get mehtods:

public class Product
 {
  public string ProductName { get; set; }
  public string ProductPrice { get; set; }
  public string Manufacturer { get; set; }
  public string Shop { get; set; }
   }


Here is my products controller class:

  public class ProductsController : ApiController
   {
      Product[] p = new Product[]
       {
       new  Product { ProductName = "JIM", Manufacturer = "Kofi",  ProductPrice = "22", Shop = "12"}
       };



// GET: api/Products/5
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);

    }

// POST: api/Products
public void 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);
    client.Execute(request);
      }
Posted
Updated 15-Aug-18 3:46am
v2

1 solution

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:

C#
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:

C#
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[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900