如何为POST端点创建链接关系?

发布于 2024-12-19 17:41:52 字数 512 浏览 4 评论 0原文

例如,在为我的网络服务构建 RESTful API 时,我尝试为客户端提供 链接关系 (这是 GET 入口点返回的内容):

<doc>
 <links>
  <link rel="self" href="/home"/>
  <link rel="post" href="/post-new-article"/>
 </links>
</doc>

我希望客户明白,为了发布新文章,他必须使用 /post-new-article 提交 POST 请求代码>“文本”作为查询 范围。

但我在文档中没有提及任何关于“POST”的内容,也没有告诉他我期望的是哪个HTTP查询参数。我应该如何以及在哪里提供此信息?是否有任何事实上的标准/惯例?

While building a RESTful API of my web service I'm trying to give clients link relations, for example (this is what a GET entry point returns):

<doc>
 <links>
  <link rel="self" href="/home"/>
  <link rel="post" href="/post-new-article"/>
 </links>
</doc>

I'm expecting the client to understand that in order to post a new article he has to submit a POST request to /post-new-article with "text" as query parameter.

But I didn't say anything about "POST" in the document, and I didn't tell him which HTTP query parameter I'm expecting. How and where should I provide this information? Is there any de-facto standard/convention about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2024-12-26 17:41:52

技术上正确的答案是它是由文档的媒体类型定义的。例如,假设 API 提供 HTML 服务。而客户端就是浏览器。

按照惯例,当用户代理(浏览器)访问资源(由锚标记标识)时,它会向提供 API 的服务器发出 HTTP GET。假设一切按计划进行,服务器将返回该资源的另一个 HTML 表示形式。

同样,当用户代理提交表单时,默认情况下它会发出 HTTP GET ,除非 form 标记的 method 属性指示它应该 < code>POST 数据。 (详细信息此处。)

您的 API 文档应该详细指定您的 REST 服务使用的媒体类型。如果它是自定义类型,则规范应包含以下链接的规则。如果您的 API 通过 HTTP 运行,则规则将指定 GETPOSTDELETE 等以及任何其他要求/约定。

来自 Roy 本人:http://roy.gbiv.com /untangled/2008/rest-apis-must-be-hypertext-driven

The technically correct answer is that it is defined by the media type of the document. For example, assume the API serves HTML. And the client is a browser.

By convention, when a user-agent (browser) visits a resource (identified by an anchor tag) it issues an HTTP GET to the server serving the API. Assuming everything goes as planned, the server returns another HTML representation of that resource.

Likewise, when a user-agent submits a form it issues an HTTP GET by default unless the method attribute of the form tag indicates it should POST the data. (Details here.)

Your API documentation should specify in detail the media type your REST service uses. If it is a custom type then the specification should include the rules for following links. If your API runs over HTTP, then the rules would specify GET, POST, DELETE, etc. as well as any other requirements/conventions.

From Roy himself: http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文