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

查看:126
本文介绍了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...

REST的"指南"或"最佳实践"之一,适用于Web服务(

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.

总而言之,要真正实现REST风格,用于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的流行语,因此不要指望它真正是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.

因此,要真正实现RESTful,除了正确使用HTTP方法之外,还有很多工作要做.如果某个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天全站免登陆