普通 API 和 REST API 的输出之间的区别 [英] Difference between an Output of a normal API and a REST API

查看:27
本文介绍了普通 API 和 REST API 的输出之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

REST API 和普通 API(打印 JSON 响应)有什么区别?

What is the difference between a REST API and a normal API (which prints a JSON response)?

推荐答案

完全没有区别.REST 描述了一种与 HTTP 服务器交互的方式,而不是服务器应该返回什么作为响应.大多数 Web 应用程序通过 POST 或 GET 请求与服务器端交互,其中包含在 POST 的表单提交或 GET 的查询字符串中完成请求所需的任何附加信息.因此,如果您想从服务器中删除某些内容,他们通常会使用包含指定资源的数据以及删除它的指令的表单执行 POST.

There is no difference at all. REST describes a way of interacting with a HTTP server, not what the server should return in response. Most web apps interact with the server side by POST or GET requests with any additional information needed to fulfil the request in a form submission for POST or the query string for GET. So if you want to delete something from the server they typically do POST with a form that contains data that specifies a resource along with an instruction to delete it.

但是,HTTP 实现了 GET 或 POST 以外的方法(也称为动词).它还实现了 HEAD(返回与 GET 相同的标头,但没有响应正文)、PUT(获取请求正文并将其内容存储在 PUT 请求发送到的任何 URL 中),以及DELETE(删除指定 URL 中存在的任何资源).REST 接口只是使用这些附加动词将请求的含义传达给服务器.

However, HTTP implements methods (also known as verbs) other than GET or POST. It also implements, amongst others, HEAD (return the same headers you would have done for a GET, but with no response body), PUT (Take the request body and store its content at whatever URL the PUT request was made to), and DELETE (Delete whatever resource exists at the specified URL). A REST interface simply makes use of these additional verbs to convay the meaning of the request to the server.

浏览器通常只支持普通"(非 XHR)请求的 GET 和 POST,但像 Curl 这样的工具可以发出完整的 HTTP 动词集.您还可以在基于 XHR 的技术(如 AJAX)中使用其他动词.

Browsers typically only support GET and POST for "normal" (non-XHR) requests, but tools like Curl can issue the full set of HTTP verbs. You can also use additional verbs with XHR-based techniques such as AJAX.

您仍然需要提供传统的非 REST API 供浏览器使用,除非您将 javascript 和 XHR 支持作为使用您的应用程序的要求.

You will still have to provide a traditional non-REST API for browsers to use, unless you're making javascript and XHR support a requirement for using your app.

这篇关于普通 API 和 REST API 的输出之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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