简体   繁体   English

REST API:设计API,允许用户提交POST内容并获取一些值,而不是创建ID作为响应

[英]REST API: Design API to let user submit POST something and GET some value other than created Id in response

It's bit lengthy but simple to understand so please read it. 它有点冗长,但易于理解,因此请阅读。

We have a feedback form where the user fills the complaint/suggestions. 我们有一个反馈表,用户可以在其中填写投诉/建议。 To fill a complaint, the user needs to fill his/her {Name, Mobile Number, ZipCode}. 要填写投诉,用户需要填写他/她的{姓名,手机号码,邮政编码}。 As a response to this complaint, we show our nearest call center number based on user's pin code. 针对此投诉,我们会根据用户的个人识别码显示我们最近的呼叫中心号码。

Once the user submits the feedback [name, mobile, zip code], the complete process is: 用户提交反馈[名称,手机,邮政编码]后,完整的过程为:

  • The user sees nearest customer care contact number upfront on the screen. 用户可以在屏幕上看到最近的客户服务联系电话。
  • New Complaint is registered in the database. 新投诉已在数据库中注册。

This process needs to be integrated across platforms (Mobile, Desktop, Apps) and we need to design Restful APIs for this. 这个过程需要跨平台(移动,桌面,应用程序)集成,我们需要为此设计Restful API。 Few choices: 几种选择:

  1. GET /api/complaint/?mobile={mobile}&name={name}&zip={zipcode}. GET / api / complaint /?mobile = {mobile}&name = {name}&zip = {zipcode}。 This API would send back nearest call center number in response, save record in complaint table. 该API将发回最近的呼叫中心号码作为响应,并将记录保存在投诉表中。 But this does not look restful as we are adding database records with GET request. 但这看起来并不轻松,因为我们正在使用GET请求添加数据库记录。
  2. POST /api/complaint/ with {mobile},{name},{zipcode} in the request body. 通过请求正文中的{mobile},{name}和{zipcode} POST / api / complaint /。 This too can do all above things but the problem here is that as per rest where we send back new created object in POST response, here we would send back nearest customer call center number which has nothing to do with complaint record creation in the database. 这也可以完成上述所有操作,但是这里的问题是,按照其他情况,我们在POST响应中发回新创建的对象,在这里我们将发回最近的客户呼叫中心号码,这与数据库中的投诉记录创建无关。
  3. Create two separate APIs, first POST /api/complaint/ with {mobile},{name} that would insert a record in the complaint table. 创建两个单独的API,第一个使用{mobile} {name}的POST / api / complaint /将在投诉表中插入一条记录。 And second, create GET /api/callcenter/?zip={zipcode} which returns back the customer care number of that zipcode. 其次,创建GET / api / callcenter /?zip = {zipcode},它返回该邮政编码的客户服务编号。 There are few problems here like two APIs are decoupled so what if POST is unsuccessful while GET is executed OR vice versa? 这里几乎没有问题,例如两个API已解耦,那么如果在执行GET时POST不成功怎么办,反之亦然呢? Also, hitting these two sequentially would be slow? 另外,连续打这两个会很慢吗?

Which of these 3 is correct OR is there any 4th way to solve this problem? 这3个中的哪一个是正确的,或者有第四种方法可以解决此问题?

I actually like the 3rd option. 我实际上喜欢第三个选项。 A failed post shouldn't affect the ability to get the nearest call center right? 发帖失败不应该影响正确联系最近的呼叫中心的能力吗? A failed post would be a problem in the second option as well. 失败的帖子在第二个选项中也是一个问题。 These call centers should already be in database. 这些呼叫中心应该已经在数据库中。 I'm sure you'll have some code/subroutine to calculate the shortest distance between any given zip code and all of the call centers. 我确定您会有一些代码/子例程来计算任何给定的邮政编码和所有呼叫中心之间的最短距离。 And in the third option, even though a complaint wouldn't be filed in a failed post, at least the user could get the number of the nearest call center to continue to get help 在第三个选项中,即使不会在失败的帖子中提出投诉,至少用户也可以获取最近的呼叫中心的号码以继续获得帮助

Teach a man to fish: almost every question in of the form "what is the REST way to do X?" 教人钓鱼:几乎每个问题都以“ REST是X的方式是什么?”的形式出现。 has the answer "figure out how to do it with html links and forms, and match that pattern as closely as you can." 答案是“找出如何使用html链接和表单,并尽可能地匹配该模式。”

the problem here is that as per rest where we send back new created object in POST response 这里的问题是,按照其他方式,我们在POST响应中发回新创建的对象

If that wasn't true, would it make your life easier? 如果那不是真的,那会让您的生活更轻松吗? If so, good news.... 如果是这样,那么好消息。

  1. REST doesn't say anything about "create"; REST没有说“创建”。 it says that resources should conform to the uniform interface. 它说资源应该符合统一的接口。

  2. If you are communicating with your API via http(s), the uniform interface is defined by RFC 7231 (not REST; REST is an architectural style, the Web is a reference application built using that style). 如果您通过http(s)与API通信,则统一接口由RFC 7231定义(不是REST; REST是一种体系结构样式,Web是使用该样式构建的参考应用程序)。

  3. HTTP does not say that POST must equal create. HTTP并不表示POST必须等于create。

The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics. POST方法要求目标资源根据资源自身的特定语义来处理请求中包含的表示形式。

  1. The HTTP specification doesn't say you have to return the created object. HTTP规范没有说您必须返回创建的对象。 In fact, it says 200 OK means that you return something different: 实际上,它说200 OK表示您返回不同的内容:

a representation of the status of, or results obtained from the action 行动状态或结果的表示

In your case, the status of your action is "congratulations, our attempt to find the closest care center to you succeeded. here it is...." Yes, this has nothing to do with the fact that you created a complaint record in the database. 对于您而言,您的行为状态是“恭喜,我们尝试找到离您最近的护理中心成功。这是...。”是的,这与您在以下位置创建投诉记录无关数据库。 From the point of view of the web client, there is no database . 从Web客户端的角度来看, 没有数据库 Just the resources and the uniform interface. 只是资源和统一的界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM