什么是最好的/常见的 RESTful url 动词和动作? [英] What are the best/common RESTful url verbs and actions?

查看:33
本文介绍了什么是最好的/常见的 RESTful url 动词和动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找有关最佳和最常见 RESTful url 操作的一些信息.

I'm trying to find some info on the best and most common RESTful url actions.

例如,您使用什么 url 来显示项目的详细信息、编辑项目、更新等.

for example, what url do you use for displaying the details of an item, for editing the item, updating, etc.

/question/show/<whatever>
/question/edit/<whatever>
/question/update/<whatever> (this is the post back url)
/question/list   (lists the questions)

嗯.感谢任何人的帮助:)

hmm. thanks to anyone helping out :)

推荐答案

使用 URL 来指定您的对象,而不是您的操作:

请注意,您首先提到的不是 RESTful:

Note what you first mentioned is not RESTful:

/questions/show/<whatever>

相反,您应该使用您的 URL 来指定您的对象:

Instead, you should use your URLs to specify your objects:

/questions/<question>

然后您对该资源执行以下操作之一.

Then you perform one of the below operations on that resource.

获取:

用于获取资源,查询资源列表,也用于查询资源的只读信息.

Used to obtain a resource, query a list of resources, and also to query read-only information on a resource.

获取问题资源:

GET /questions/<question> HTTP/1.1
Host: whateverblahblah.com

要列出所有问题资源:

GET /questions HTTP/1.1
Host: whateverblahblah.com

发布:

用于创建资源.

注意以下错误:

POST /questions/<new_question> HTTP/1.1
Host: whateverblahblah.com

如果 URL 尚未创建,则在指定名称时不应使用 POST 来创建它.这应该导致资源未找到错误,因为尚不存在.您应该先将资源放在服务器上.您可能会争辩说,通过创建一个新问题,您也在更新/questions 资源,因为它现在会在其问题列表中再返回一个问题.

If the URL is not yet created, you should not be using POST to create it while specifying the name. This should result in a resource not found error because does not exist yet. You should PUT the resource on the server first. You could argue that by creating a new question, you are also updating the /questions resource as it would now return one more question in its list of questions.

您应该执行以下操作以使用 POST 创建资源:

You should do something like this to create a resource using POST:

POST /questions HTTP/1.1
Host: whateverblahblah.com

请注意,在这种情况下,未指定资源名称,新对象 URL 路径将返回给您.

Note that in this case the resource name is not specified, the new objects URL path would be returned to you.

删除:

用于删除资源.

DELETE /questions/<question> HTTP/1.1
Host: whateverblahblah.com

PUT:

用于在您指定资源 URL 时创建或覆盖它.

Used to create a resource, or overwrite it, while you specify the resources URL.

对于新资源:

PUT /questions/<new_question> HTTP/1.1
Host: whateverblahblah.com

覆盖现有资源:

PUT /questions/<existing_question> HTTP/1.1
Host: whateverblahblah.com

...是的,它们是一样的.PUT 通常被描述为编辑"方法,因为通过用稍微改变的版本替换整个资源,您编辑了客户端下次执行时将获得的内容.

...Yes, they are the same. PUT is often described as the 'edit' method, as by replacing the entire resource with a slightly altered version, you have edited what clients will GET when they next do.

在 HTML 表单中使用 REST:

HTML5 规范定义了 GET 和表单元素的POST.

方法内容属性是一个枚举属性,具有以下关键字和状态:

The method content attribute is an enumerated attribute with the following keywords and states:

  • 关键字GET,映射到状态GET,表示HTTP GET方法.
  • 关键字 POST,映射到状态 POST,表示 HTTP POST 方法.

<小时>

从技术上讲,HTTP 规范并不仅限于这些方法.从技术上讲,您可以随意添加任何想要的方法,但实际上,这不是一个好主意.这个想法是每个人都知道你使用 GET 来读取数据,所以如果你决定使用 READ 会混淆问题.话说……


Technically, the HTTP specification does not limit you to only those methods. You are technically free to add any methods you want, in practice though, this is not a good idea. The idea is that everyone knows that you use GET to read the data, so it will confuse matters if you decide to instead use READ. That said...

补丁:

这是在正式 RFC 中定义的方法.它旨在用于当您希望仅向资源发送部分修改时,它的使用方式与 PUT 非常相似:

This is a method that was defined in a formal RFC. It is designed to used for when you wish to send just a partial modification to a resource, it would be used much like PUT:

PATCH /questions/<new_question> HTTP/1.1
Host: whateverblahblah.com

区别在于 PUT 必须发送整个资源,无论它与实际更改的内容相比有多大,而 PATCH 您可以只发送更改.

The difference is PUT has to send the entire resource, no matter how big it is compared to what's actually changed, whilst PATCH you can send just the changes.

这篇关于什么是最好的/常见的 RESTful url 动词和动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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