REST API 真的是 RESTful 吗? [英] Are REST API's really RESTful?

查看:30
本文介绍了REST API 真的是 RESTful 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个游戏的新手,所以我可能会误解一些事情.实际上,如果有人告诉我我误解了事情,那将是一个帮助.也许这个人会很体贴,给我指明正确的道路.但是...

I'm new to this game so I might be misunderstanding things. Actually, if someone tells me that I'm misunderstanding things, it will be a favor. Maybe this person will be considerate enough to show me the right path. But...

适用于 Web 服务的 REST 的指南"或最佳实践"之一(http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services) 是您应该使用正确的HTTP 方法调用 REST API 时(我是不是误解了?).

One of the "guidelines" or "best practices" of REST as it applies to Web Services (http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services) is that you should use the proper HTTP methods when making calls (did I misunderstand it?) to REST API's.

但是查看网络上的许多 API 实现,我看到的是 100% 的调用实际上是 GET 调用,这取决于它们的URI,将被 API 解释为 HTTP 动词或方法之一.

But looking at many API implementations on the web, what I see is that 100% of the calls made to them are actually GET calls that, depending on their URI, will be interpreted by the API as being of one of the HTTP verbs or methods.

例如,查看 Twitter 的 REST API 文档(https://dev.twitter.com/rest/public),原则上只定义了两个动词/方法(GET 和 POST),实际上所有调用都作为 GET 发送,并且基于 GET 调用中的 URI,由 API 解释并采取行动.

So, for example, looking at the REST API documentation for Twitter (https://dev.twitter.com/rest/public) which, in principle, only defines two verbs/methods (GET and POST), actually have all calls sent as GET and, based on the URI in the GET call, are interpreted by the API and acted upon.

示例:

获取状态/查找:https://api.twitter.com/1.1/statuses/lookup.json

POST 状态/更新(PUT?):https://api.twitter.com/1.1/statuses/update.json

在这两种情况下,调用本身都是使用 GET 进行的,而 URI 的最后一部分将其定义为真正的 GET 或 POST.

In both cases, the call itself was made using GET and the last part of the URI defined it as a real GET or as a POST.

总而言之,要实现真正的 RESTful,用于 Web 服务的 REST API 的客户端实现不应该使用正确的 HTTP 动词/方法吗?

In summary, to be truly RESTful, shouldn't client side implementations of REST API's for web services use the proper HTTP verbs/methods?

我错过了什么?

推荐答案

你遗漏了很多,但别担心,大多数人都是.

You're missing a lot, but don't worry about it, most people are.

事实上,互联网上公开的所谓 REST API 很少是真正的 RESTful,主要是因为它们不是 超文本驱动.REST 成为一个流行词来指代任何非 SOAP 的 HTTP API,所以不要指望一个 API 真的是 RESTful,因为它说它是一个 REST API.我建议阅读这个答案.

The fact is that very few so-called REST APIs publicly available on the internet are really RESTful, mostly because they are not hypertext driven. REST became a buzzword to refer to any HTTP API that isn't SOAP, so don't expect for an API to really be RESTful just because it says it's a REST API. I recommend reading this answer.

根据我的经验,大多数 API 开发人员并不知道真正的 REST 是什么,并且相信任何使用 HTTP 并避免在 URI 中使用动词的 HTTP API 都是 REST.

From my experience, most API developers aren't aware what REST really is and believe any HTTP API that uses HTTP and avoids verbs in URIs is REST.

REST 由一组约束定义.其中包括统一接口,简单来说就是不应该改变底层协议的预期行为.REST 不与任何特定协议耦合,但由于它与 HTTP 一起使用很常见,因此有时会令人费解.

REST is defined by a set of constraints. Among them is the uniform interface, which in simple terms means that you should not change the expected behavior of the underlying protocol. REST isn't coupled to any particular protocol, but since it's common to be used with HTTP, they get convoluted sometimes.

HTTP 为 GET、POST、PUT、DELETE、PATCH 和 HEAD 方法定义了非常明确的语义,而 POST 方法的语义由服务器决定.理想情况下,REST API 应该完全按照 RFC 7231 中确定的方式响应 POST 以外的方法,但正如您所注意到的,有许多 API 称自己为 REST,但并不这样做.发生这种情况的原因有很多.有时对正确的语义存在简单的误解,或者是为了保持一致性,或者是因为向后兼容不支持所有方法的中介,以及许多其他原因.

HTTP has very well defined semantics for the GET, POST, PUT, DELETE, PATCH and HEAD methods, and the POST method has its semantics determined by the server. Ideally, a REST API should respond to the methods other than POST exactly as determined in the RFC 7231, but as you noticed, there are many APIs who call themselves REST but don't do that. This happens for many reasons. Sometimes there's a simple misunderstanding about the correct semantics, or it's done to keep consistency, or because of backwards compatibility with intermediaries that don't support all methods, and many other reasons.

因此,除了正确使用 HTTP 方法之外,还需要做更多的工作才能真正实现 RESTful.如果 API 甚至没有做到这一点,它就需要找到另一个流行语,因为它绝对不是 REST.

So, there's a lot more that has to be done to be truly RESTful other than using the HTTP methods correctly. If an API doesn't get even that right, it needs to find another buzzword, because it's definitely not REST.

这篇关于REST API 真的是 RESTful 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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