简体   繁体   English

REST 和 API 有什么区别?

[英]What is difference between REST and API?

I want to know the main difference between REST and API. Sometimes I see REST API in programming documents, then is REST or API same as REST API?我想知道REST和API之间的主要区别。有时我在编程文档中看到REST API,那么REST或API与8834142818316308551821688相同吗? I would like to know more about relation between REST, API and REST API.我想知道更多关于 REST、API 和 REST API 之间的关系。

REST is a type of API. REST 是一种 API。 Not all APIs are REST, but all REST services are APIs.并非所有 API 都是 REST,但所有 REST 服务都是 API。

API is a very broad term. API 是一个非常广泛的术语。 Generally it's how one piece of code talks to another.通常这是一段代码如何与另一段代码对话。 In web development API often refers to the way in which we retrieve information from an online service.在 Web 开发中,API 通常指的是我们从在线服务中检索信息的方式。 The API documentation will give you a list of URLs, query parameters and other information on how to make a request from the API, and inform you what sort of response will be given for each query. API 文档将为您提供 URL 列表、查询参数和其他有关如何从 API 发出请求的信息,并告知您将为每个查询提供什么样的响应。

REST is a set of rules/standards/guidelines for how to build a web API. REST 是一组关于如何构建 Web API 的规则/标准/指南。 Since there are many ways to do so, having an agreed upon system of structuring an API saves time in making decisions when building one, and saves time in understanding how to use one.由于有很多方法可以做到这一点,拥有一个商定的构建 API 的系统可以节省构建 API 时做出决策的时间,并节省理解如何使用 API 的时间。

Other popular API paradigms include SOAP and GraphQL.其他流行的 API 范例包括 SOAP 和 GraphQL。

REST mostly just refers to using the HTTP protocol the way it was intended. REST 主要是指按照预期的方式使用 HTTP 协议。 Use the GET HTTP method on a URL to retrieve information, possibly in different formats based on HTTP Accept headers.对 URL 使用GET HTTP 方法来检索信息,可能基于 HTTP Accept标头采用不同的格式。 Use the POST HTTP method to create new items on the server, PUT to edit existing items, DELETE to delete them.使用POST HTTP 方法在服务器上创建新项目,使用PUT编辑现有项目,使用DELETE删除它们。 Make the API idempotent, ie repeating the same query with the same information should yield the same result.使 API 具有幂等性,即使用相同的信息重复相同的查询应该产生相同的结果。 Structure your URLs in a hierarchical manner etc.以层次结构等方式构建您的 URL。

REST just is a guiding principle how to use URLs and the HTTP protocol to structure an API. REST 只是如何使用 URL 和 HTTP 协议来构建 API 的指导原则。 It says nothing about return formats, which may just as well be JSON.它没有说明返回格式,也可能是 JSON。

That is opposed to, for example, APIs that send binary or XML messages to a designated port, not using differences in HTTP methods or URLs at all.例如,这与将二进制或 XML 消息发送到指定端口的 API 完全相反,根本不使用 HTTP 方法或 URL 的差异。

There is no comparison in REST and API, REST is an API type. REST 和 API 没有可比性,REST 是一种 API 类型。

API, in general, is a set of protocols deployed over an application software to communicate with other software components (Like browser interacting with servers) and provide an interface to services which the application software offers to several live consumers.一般来说,API 是一组部署在应用程序软件上的协议,用于与其他软件组件(如浏览器与服务器交互)进行通信,并为应用程序软件提供给多个实时消费者的服务提供接口。

And Rest is a form of principle which an API follows in which the server provides information whatever the client desires to interact with services. Rest 是 API 遵循的一种原则形式,其中服务器提供客户端希望与服务交互的任何信息。

REST basically is a style of web architecture that governs the behavior of clients and servers. REST 基本上是一种管理客户端和服务器行为的 Web 架构风格。 While API is a more general set of protocols and is deployed over the software to help it interact with some other software. API 是一组更通用的协议,部署在软件上以帮助它与其他一些软件进行交互。 REST is only geared towards web applications. REST 仅面向 Web 应用程序。 And mostly deals with HTTP requests and responses.并且主要处理 HTTP 请求和响应。 This makes it practically usable by any programming language and easy to test.这使得它实际上可以被任何编程语言使用并且易于测试。

API is basically a set of functions and procedures which allow one application to access the feature of other application API基本上是一组函数和过程,允许一个应用程序访问另一个应用程序的功能

REST is a set of rules or guidelines to build a web API. REST 是一组用于构建 Web API 的规则或指南。 It is basically an architectural style for networked applications on the web which is limited to client-server based applications.它基本上是网络上的网络应用程序的架构风格,仅限于基于客户端-服务器的应用程序。

Read more at: https://www.freelancinggig.com/blog/2018/11/02/what-is-the-difference-between-api-and-rest-api/阅读更多信息: https : //www.freelancinggig.com/blog/2018/11/02/what-is-the-difference-between-api-and-rest-api/

API is an acronym for Application Programming Interface and defines a set of structures (ie classes) one has to implement in order to interact with a service the API was exposed for. API 是Application Programming Interface的首字母缩写词,它定义了一组必须实现的结构(即类),以便与公开 API 的服务进行交互。 APIs usually expose operations that can be invoked including any required or supported arguments as well as the expected responses. API 通常公开可以调用的操作,包括任何必需或支持的参数以及预期的响应。 Classical examples here are Corba IDL, SOAP or RMI in the Java ecosystem but also RPC-like usages of Web systems specified in documentation like Swagger or OpenAPI.此处的经典示例是 Java 生态系统中的 Corba IDL、SOAP 或 RMI,以及 Swagger 或 OpenAPI 等文档中指定的 Web 系统的类似 RPC 用法。

REST (REpresentational State Transfer) on the contrary was specified by Fielding in his doctoral thesis where he analyzed how the whole user interactions occurs on the Web.相反,REST(REpresentational State Transfer)是 Fielding 在他的博士论文中指定的,他分析了整个用户交互如何在 Web 上发生。 He realized that on the Web only a transport protocol, a naming scheme for stuff as well as a well defined exchanged format is needed to exchange messages or documents.他意识到在 Web 上只需要一个传输协议、一个东西的命名方案以及一个明确定义的交换格式来交换消息或文档。 These three parts therefore define the interface to interact with peers in such a ecosystem.因此,这三个部分定义了与此类生态系统中的对等方交互的接口。 The transport layer is covered by HTTP while the naming scheme is defined by URI/IRI.传输层由 HTTP 覆盖,而命名方案由 URI/IRI 定义。 Contrary to traditional RPC protocols which usually only support one syntax, REST is actually independent from a particular syntax.与通常只支持一种语法的传统 RPC 协议相反,REST 实际上独立于特定的语法。 To upkeep interoperability both client and server though need do negotiate about it, which HTTP itself supports through the Accept request and Content-Type response headers.为了保持互操作性,客户端和服务器都需要进行协商,HTTP 本身通过Accept请求和Content-Type响应标头支持这一点。 As long as client and server support HTTP, URI/IRI and a set of negotiated representation formats, defined by backing hypermedia capable media-types, they will be able to interact with each other.只要客户端和服务器支持 HTTP、URI/IRI 和一组由支持超媒体的媒体类型定义的协商表示格式,它们就能够相互交互。 In a more narrow sense REST therefore has no API other than HTTP, URI/IRI and the respective media types.因此,在更狭义的意义上,REST 除了 HTTP、URI/IRI 和相应的媒体类型之外没有其他 API。

However, things are unfortunately not that easy.然而,不幸的是事情并没有那么容易。 Most people unfortunately understand something very different in terms of REST or REST API .不幸的是,大多数人对RESTREST API理解非常不同。 While URIs should not convey any semantics itself, after all they are just pointers to a resource, plenty of programmers attribute more importance to URIs than they should.虽然 URI 本身不应该传达任何语义,毕竟它们只是指向资源的指针,但许多程序员认为 URI 的重要性比他们应有的要高。 Some clients ie will attempt to extract some knowledge off of URIs or consider URIs to return responses that represent a certain type .一些客户端 ie 会尝试从 URI 中提取一些知识,或者考虑使用 URI 来返回代表某种类型的响应 Ie it may seem natural to consider an URI such as https://api.acme.org/users/1 to return a representation that describes a particular user of that particualar system.即,考虑使用诸如https://api.acme.org/users/1的 URI 来返回描述该特定系统的特定用户的表示似乎很自然。 An external documentation may specify that a JSON structure is returned that follows a given template such as外部文档可能会指定返回遵循给定模板的 JSON 结构,例如

{
  "id": 1,
  "firstName": "Roman",
  "lastName": "Vottner",
  "role": "Admin",
  ...
}

can be expected, however, such a thing is closer to RPC than it is to REST.然而,可以预料,这样的事情更接近于 RPC 而不是 REST。 Neither is the response self-descriptive, as required by REST, nor does it follow a representation format that follows a well defined media type that defines the syntax and each of the elements that may form a message.响应既不是 REST 所要求的自描述性的,也不是遵循明确定义的媒体类型的表示格式,该媒体类型定义了语法和可能形成消息的每个元素。 Clients therefore are usually tailor-made for exactly one particular system (or REST API if you will) and can't be used to interact with different systems out of the box without further manual integration/updates.因此,客户端通常是为一个特定系统(或 REST API,如果您愿意)量身定制的,并且不能在没有进一步手动集成/更新的情况下用于与开箱即用的不同系统进行交互。 External documentation such as OpenAPI or Swagger are used to describe the available endpoints, the payload-templates that a server will be able to process as well as the expected responses, depending on the input. OpenAPI 或 Swagger 等外部文档用于描述可用的端点、服务器将能够处理的有效负载模板以及预期的响应,具体取决于输入。 These documentation therefore is the truth and thus defines the API a client can look up or even use to autogenerate stub classes to interact with the server-side, similar to SOAP.因此,这些文档是真实的,因此定义了客户端可以查找甚至用于自动生成存根类以与服务器端交互的 API,类似于 SOAP。

I therefore don't agree with the answer given by dave.因此,我不同意戴夫给出的答案。 While for RPC systems or the common understood term of REST API his definition may be suitable, for actual REST architectures his explanation isn't fitting at all and thus, IMO at least, not correct either.虽然对于 RPC 系统或REST API的通用理解术语,他的定义可能是合适的,但对于实际的 REST 架构,他的解释根本不合适,因此,至少 IMO 也不正确。 REST isn't a collection of rules, standards and/or guidelines. REST 不是规则、标准和/或指南的集合。 It is a set of few constraints that just ensure that peers in such an architecture avoid coupling, support future evolution and become more robust to change.它是一组少数约束,可以确保此类架构中的对等方避免耦合,支持未来的演进并变得更加健壮以应对变化。

我通过这个(链接到他解释差异的那部分)视频了解了 API 和 REST API 之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM