Restsharp不返回任何数据获取请求和发布请求 [英] Restsharp not returning any data get request and post request
问题描述
我正在学习如何使用RestSharp来进行web api调用。当我使用Postman发出get请求时它返回200并且在正文中它给我null而不是json当我用Postman发出一个帖子请求它返回204(没有内容)。
所有建议欢迎。谢谢。
我尝试过:
这是我的产品带有集合的类和获取方法:
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);
}
推荐答案
我还没有用过RestSharp
之前,但这是我的看法:
首先,你的代码没有意义。您正在创建Web API
端点,并且您正在端点实现中调用/使用相同的端点。正如我在google搜索后所了解的那样,RestSharp是一个REST Http客户端。这意味着您单独构建Web API并在.NET代码中使用RestSharp来使用它们。这是一篇很好的文章,演示了我在说什么: ASP.NET Web API, RestSharp和模型错误消息| DotNetCurry [ ^ ]
其次,确认您将内容类型
设置为邮递员配置中的application / json
。
三,你的获取
结束 - point没有返回,这就是为什么你得到null
。你应该有类似的东西:
I haven't usedRestSharp
before but here's my take:
First off, your code doesn't make sense. You're creating aWeb 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 thecontent-type
toapplication/json
in Postman configuration.
third, yourGet
end-point doesn't have a return that's why you are gettingnull
. 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;
}
第四,您的帖子
端点无效,因此它返回 204
(无内容)。您可以返回 IHttpActionResult
之类的内容:
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));
}
最后,在设计 REST API
时Web API,我建议给这篇简短的文章读一读: ASP.NET核心和Web API:用于管理异常和一致响应的自定义包装器 [ ^ ]
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[^]
这篇关于Restsharp不返回任何数据获取请求和发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!