它是正确使用POST而不是获取获取在网页API数据 [英] Is it correct to use Post instead of Get to fetch data in Web API

查看:265
本文介绍了它是正确使用POST而不是获取获取在网页API数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前通过ASP.Net的WebAPI技术创建RESTful API。我已经涉及到的WebAPI 2个问题

我曾做过如下:


  1. 创建以下方法Controller类:


      

    公开的Htt presponseMessage PostOrderData(OrderParam订单信息)



  2. 基于参数订单信息,查询SQL Server和拿到订单的清单。

  3. 将Response.Content的集合对象:


 列表<订单> ordList =新的List<订单>();
//充满从SQL查询结果的ordList
VAR响应= Request.CreateResponse<名单,LT;订单>>(的HTTPStatus code.OK,ordList);



  1. 在客户端,


  OrderParam ordparam =新OrderParam();
响应= client.PostAsJsonAsync(API /命令,ordparam)。结果;
如果(response.IsSuccessStatus code)
{
清单<订单> MYDATA = response.Content.ReadAsAsync<名单,LT;订单>>()结果。
}


所以问题:这是精细的数据发布到服务器获取数据后的数据即使用insted的获取是正确的?有没有办法任何不利? (一个缺点是:我会不能够直接从浏览器查询)我用邮这里,因为参数OrderParam可能在将来扩展和有可能是由于在URL长度增加,由于这个问题。

第二个问题是:我已经使用类参数和返回对象即OrderParam和订单。现在,这个网页API的消费者(客户)是不同的客户,他们会通过。NET(C#)或通过jQuery的/ JS消耗API。所以,我们需要通过手动含有OrderParam和订单班的认定中的每个客户端这个类文件?每一次客户端发送的时候会有上述类别的任何变化?

提前结果谢谢
沙阿


解决方案

通常没有。

POST是不是安全的也不是幂 - 这样的不能被缓存。它的目的是用于你在哪里的服务器上更改的状态的情况。

如果你有一个大critieria,你需要重新设计,但在大多数情况下,URL片段或查询字符串PARAMS工作。看看的OData它使用的查询字符串非常复杂的查询和使用GET。

对于第二个问题,也没什么。服务器可以公开模式(类似于WSDL)或文档但是不应该知道的客户端。

I am currently creating Restful API through ASP.Net WebAPI technology. I have 2 questions related to WebAPI

I had done below:

  1. Created below method in Controller class:

    public HttpResponseMessage PostOrderData(OrderParam OrderInfo)

  2. Based on Parameter OrderInfo, Query the SQL Server and get list of orders.
  3. Set the Response.Content with the collection object:

List<Orders> ordList = new List<Orders>();  
//filled the ordList from SQL query result  
var response = Request.CreateResponse<List<Orders>>(HttpStatusCode.OK, ordList);

  1. On Client side,

OrderParam ordparam = new OrderParam();  
response = client.PostAsJsonAsync("api/order", ordparam).Result;  
if (response.IsSuccessStatusCode)  
{  
List<Orders> mydata = response.Content.ReadAsAsync<List<Orders>>().Result;  
}

So question: is it fine to Post the data to server to Get the data i.e. usage of Post data insted of Get is correct? Is there any disadvantage in approach? (One disadvantage is: I will not able to query directly from browser) I have used Post here because parameter "OrderParam" might extend in future and there might be problem due to increase in Length of URL due to that.

2nd Question is: I have used classes for parameter and for returning objects i.e. OrderParam and Orders. Now consumer (clients) of this web api are different customers and they will consume API through .Net (C#) or through Jquery/JS. So do we need to pass this class file containing defination of OrderParam and Orders classes manually to each client? and send each time to client when there will be any change in above classes?

Thanks in advance
Shah

解决方案

Typically no.

POST is not safe nor idempotent - as such cannot be cached. It is meant to be used for cases where you are changing the state on the server.

If you have a big critieria, you need to redesign but in most cases, URL fragments or querystring params work. Have a look at OData which uses querystring for very complex queries and uses GET.

With regard to second question, also no. Server can expose schema (similar to WSDL) or docs but should not know about the client.

这篇关于它是正确使用POST而不是获取获取在网页API数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆