[英]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: 用户提交反馈[名称,手机,邮政编码]后,完整的过程为:
This process needs to be integrated across platforms (Mobile, Desktop, Apps) and we need to design Restful APIs for this. 这个过程需要跨平台(移动,桌面,应用程序)集成,我们需要为此设计Restful API。 Few choices:
几种选择:
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....
如果是这样,那么好消息。
REST doesn't say anything about "create"; REST没有说“创建”。 it says that resources should conform to the uniform interface.
它说资源应该符合统一的接口。
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是使用该样式构建的参考应用程序)。
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方法要求目标资源根据资源自身的特定语义来处理请求中包含的表示形式。
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.