为什么在 REST 实现的 URI 中包含动作动词会违反协议? [英] Why does including an action verb in the URI in a REST implementation violate the protocol?

查看:32
本文介绍了为什么在 REST 实现的 URI 中包含动作动词会违反协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有必要了解为什么在 URI 中包含动作动词会违反 URI 语法的 REST 协议?当我阅读以下文章时,我感觉到太多人对动词制造了太多噪音,他们应该对内容类型制造更多噪音:

I'm finding it necessary to understand why including action verbs in the URI violates the REST protocol for URI syntax? When I read the following article, I sense that too many people are making too much noise about verbs, and that they should be making more noise about content types:

RestWiki:最小方法

在一个完美的世界中,客户端浏览器都支持 GET、POST、PUT 和 DELETE 请求操作.但是,仅支持 GET 和 POST,这意味着我们无法通过在 URL 中使用常见的动作动词(如查看、创建、编辑和删除)来尝试识别应该是 PUT 和 DELETE 的操作.

In a perfect world, client browsers would all support GET, POST, PUT and DELETE for request operations. However, only GET and POST are supported, which means we're stuck trying to identify operations that should be PUT and DELETE by using common action verbs in the URL like view, create, edit, and delete.

这如何违反 REST 架构原则的精神?在 URL 中添加删除"之类的内容而不是使用删除"会遇到什么障碍?

How does this violate the spirit of the REST architectural principles, and what is the roadblock you experience by putting something like "delete" into your URL instead of using "deletion"?

推荐答案

关于 URI 的指导的唯一正当理由是鼓励正确使用 REST 动词.如果根据 HTTP 标准,请求执行的操作与客户端的期望一致,那么 url 包含的内容实际上并不重要.

The only valid reason for the guidance around URIs is to encourage proper usage of the REST verbs. If a request performs an action that is consistent with the client's expectation as per the HTTP standards then it really does not matter what the url contains.

基于名词命名 url 可以很自然地创建与 GET、PUT、POST 和 DELETE 的预期目的一致的行为.

Naming urls based on nouns makes it natural to create behaviour that is consistent with the intended purpose of GET, PUT, POST and DELETE.

当你把动词放在 URL 中时,它会变得非常混乱,因为 http 动词通常会与 URL 中的动词有矛盾的行为.REST 规则规定您必须遵守 HTTP 动词,但通常 url 更具描述性,因此可能会产生误导.

When you put verbs in the URL it can become very confusing because often the http verb will have contradictory behaviour to the one in the URL. REST rules say you must respect the HTTP verb, but usually the url is more descriptive so it can be misleading.

浏览器仅支持一部分 HTTP 动词的事实并不重要,因为即使您确实拥有对所有 HTTP 动词的完全访问权限,您仍然需要能够对其他动词建模,例如打印、关闭、确认、取消.

The fact that browsers only support a subset of HTTP verbs is not really relevant because even when you do have full access to all the HTTP verbs, you still need to be able to model other verbs, like print, close, confirm, cancel.

在谈论 REST 实现时,人们需要更多地关注内容类型而不是 URL 结构,这是绝对正确的.

You are absolutely right that people need to focus way more on content types than URL structure when talking about REST implementations.

让你的 URL 指向名词并不是 REST 的限制,而是鼓励人们掉入成功的陷阱.

Making your URLs refer to nouns is not a REST constraint, it is about encouraging people to fall into the pit of success.

这篇关于为什么在 REST 实现的 URI 中包含动作动词会违反协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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