简体   繁体   English

REST API与Web API

[英]REST API vs Web API

I am a beginner in building HTTP APIs and I seem to be confused about the difference between REST API and Web API. 我是构建HTTP API的初学者,我似乎对REST API和Web API之间的差异感到困惑。 More I read about it on the web, confusion seems to add up. 我在网络上阅读了更多有关该内容的信息,这似乎加剧了混乱。 I guess Fielding has the same issue as per this link http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven 我猜想Fielding与此链接http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven有相同的问题

I built an HTTP API at work thinking I built a REST API as everywhere I read, they were building Web/HTTP API and calling it REST. 我在工作时构建了HTTP API,以为我在阅读的任何地方都构建了REST API,他们正在构建Web / HTTP API并将其称为REST。

When I found one API adhere to HATEOAS principle was the one Github REST API https://api.github.com . 当我发现一个遵循HATEOAS原理的API就是一个Github REST API https://api.github.com I tried using it for my username at Github (GET https://api.github.com/users/vvs14 ), it returned all related links as per HATEOAS principle. 我在Github(GET https://api.github.com/users/vvs14 )上尝试使用它作为我的用户名,它按照HATEOAS原则返回了所有相关链接。

It is one of the best real-world API which is close to REST specification, IMHO. 它是接近REST规范IMHO的最佳现实世界API之一。 Although I could not understand which URI supports what operation on it and how to find it in case of any REST API if I am a consumer of it or how to tell it to the consumer if I am hosting the API? 尽管我无法理解哪个URI支持哪种操作以及在使用REST API的情况下如何找到它(如果我是它的使用者),或者如果我托管该API则如何告诉使用者?

One good blog on it is https://www.e4developer.com/2018/02/16/hateoas-simple-explanation/ . 关于它的一个不错的博客是https://www.e4developer.com/2018/02/16/hateoas-simple-explanation/

All other examples are given in most of the blogs just tell to use JSON to be REST API, put everything as Resource to be REST API and use HTTP verbs for CRUD operations to be REST API. 在大多数博客中都给出了所有其他示例,只是告诉他们使用JSON作为REST API,将所有内容都作为Resource成为REST API,并将HTTP动词用于CRUD操作作为REST API。 I don't find any to these to be true. 我发现这些都不是真的。

At my work, I use Sendgrid's Web API to send emails to clients and they call it Web API, not REST, which I think is pretty true. 在我的工作中,我使用Sendgrid的Web API向客户端发送电子邮件,他们将其称为Web API,而不是REST,我认为这是事实。

Can anyone please clear the difference between these two, with examples? 谁能举例说明清除两者之间的区别?

If Github API is a right example of REST API, how can we know which URI supports which operations as Media type is not mentioned here? 如果Github API是REST API的正确示例,那么我们如何才能知道哪个URI支持哪个操作(媒体类型)呢?

The only resource that can tell you what it supports is the resource itself. 可以告诉您支持什么的唯一资源是资源本身。 Any information a resource gives you about what other resources support is purely advisory. 资源提供的有关其他资源支持的任何信息纯粹是建议性的。 The only true way to find out is to try it and handle success/failure. 找出问题的唯一真实方法是尝试并处理成功/失败。 This is because what requests are accepted might change minute-by-minute (eg delete). 这是因为接受的请求可能会每分钟更改一次(例如,删除)。

If your API could be navigable and forms submittable in a web browser — a client that knows nothing about your API other than the starting URL and HTML format — and assuming text/html was a negotiable representation of your API, then it is RESTful. 如果您的API可以在Web浏览器中导航并且可以提交表单(客户端除了起始URL和HTML格式之外对您的API一无所知),并且假定text / html是API的可协商代表,那么它就是RESTful。 It can be RESTful even if it is not navigable in a browser but that's harder to demonstrate. 即使它不能在浏览器中导航,也可以是RESTful的,但很难演示。

You are right, there is a lot of confusion. 您是对的,有很多困惑。 Experts typically refer to 'true' REST APIs as HATEOAS or hypermedia-driven API's to avoid that confusion. 专家通常将“真正的” REST API称为HATEOAS或超媒体驱动的API,以避免混淆。 Most API's that do call themselves REST api's typically aren't. 大多数API的那些自称REST API的一般都没有。

So when talking with other engineers about REST apis, it's helpful to first clarify what everyone considers REST and not REST. 因此,在与其他工程师讨论REST api时,首先弄清每个人对REST而非REST的看法将很有帮助。 They're not bad engineers for not knowing, the term 'REST' just kind of got a life of its own , and I would say that HATEOAS is probably a lot more of a niche skill. 他们并不是不懂得的坏工程师,“ REST”这个词本身就是一种命脉,我想说HATEOAS可能更多是利基技能。

I agree with Nicholas Shank's answer that in many cases that the universal thing to do figure out if for example a DELETE works, is to actually issue the DELETE and see if it worked afterwards. 我同意尼古拉斯·尚克(Nicholas Shank)的回答,在许多情况下,普遍要做的事情是弄清楚例如DELETE起作用,实际上是发出DELETE然后看它是否起作用。

This isn't always helpful though, because many people building API's will want to not show a 'delete' button if it wouldn't work anyway. 但是,这并不总是有用的,因为许多构建API的人都希望如果无论如何都无法显示“删除”按钮。

So what is a reasonable way to tell a client that DELETE is available? 那么,告诉客户DELETE可用的合理方法是什么? The HTTP standard actually does have an Allow header that you could use to find out what methods work on a given endpoint. HTTP标准实际上确实具有Allow标头,可用于查找在给定端点上起作用的方法。 To find out what those are, you can issue an OPTIONS request. 要找出它们是什么,您可以发出一个OPTIONS请求。 Not every framework supports this out of the box, but it's a legitimate way to do this. 并非每个框架都支持开箱即用,但这是实现此目的的合法方法。

Another way to tell a client in advance is to embed this information in the resource you are accessing. 提前告知客户端的另一种方法是将此信息嵌入到您正在访问的资源中。 A couple of examples: 几个例子:

  1. link hints is a draft internet standard, that could give these hints in a variety of different places, such as HAL, the HTTP Link header or others. 链接提示是互联网标准草案,可以在各种不同的位置提供这些提示,例如HAL,HTTP Link标头或其他。 It basically suggests a universal format for this information. 它基本上建议了此信息的通用格式。
  2. If you use something like OpenAPI, you can add to your API specification which methods will and wont work. 如果使用类似OpenAPI的方法,则可以将哪些方法可以使用,哪些方法不可以使用。 This can work well for cases where you know a DELETE will simply never work, but it won't help you really well in cases where different users might have different levels of access, and some people can use DELETE and others can't. 在您知道DELETE根本无法工作的情况下,这可能会很好地工作,但是在不同用户可能具有不同访问级别,而有些人可以使用DELETE而另一些人不能使用的情况下,这将无法真正帮助您。
  3. You can embed this information in your own format, by expressing it as a set of permissions, maybe in a JSON format that your application understands to interpret whether certain actions may be performed. 您可以通过将其表达为一组权限(可以使用应用程序可以理解的JSON格式来解释是否可以执行某些操作)来以自己的格式嵌入此信息。
  4. Some HATEAOS formats explicitly embed information about what kind of actions can be taken via actions. 一些HATEAOS格式显式地嵌入有关可通过操作采取何种操作的信息。 A great example is the SIREN format. SIREN格式就是一个很好的例子。 HAL does not natively have this. HAL本身没有此功能。

Ultimately a good HATEAOS format will not only return information about the resource and relationships to others, but also will give a set of potential actions that can be taken. 最终,良好的HATEAOS格式不仅会返回有关资源和与他人之间的关系的信息,还会提供一组可以采取的潜在措施。 Most REST APIs that are HATEAOS tend to not do this, but HTML is the best example of one that does. 大多数的REST API HATEAOS往往不这样做,但HTML是一个没有最好的例子。 If there's no link, button or form to do an action, a user can't discover that action. 如果没有链接,按钮或表单来执行操作,则用户将无法发现该操作。

A link-hints example, embedded in HAL 链接提示示例,嵌入在HAL中

{
  "_links": {
    "self": {
      "href": "/orders/523",
      "hints": {
        "allow": ["GET", "DELETE"],
      }
    }
  }
}

A SIREN example SIREN示例

 {
  "class": [ "order" ],
  "properties": { 
      "orderNumber": 523, 
  },
  "actions": [
    {
      "name": "delete-order",
      "title": "Delete Order",
      "method": "DELETE",
      "href": "/orders/523",
    }
  ],
  "links": [
    { "rel": [ "self" ], "href": "/orders/523" },
  ]
}

An OPTIONS response 选项回应

HTTP/1.1 200 OK
Allow: GET, DELETE

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

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