简体   繁体   English

REST和HTTP协议有什么区别?

[英]What is the difference between REST and HTTP protocols?

什么是REST协议,它与HTTP协议有什么不同?

REST is a design style for protocols, it was developed by Roy Fielding in his PhD dissertation and formalised the approach behind HTTP/1.0, finding what worked well with it, and then using this more structured understanding of it to influence the design of HTTP/1.1. REST是协议的一种设计风格,它是由Roy Fielding在其博士论文中开发的,并正式化了HTTP / 1.0背后的方法,找到了适合的方法,然后利用对它的更结构化的理解来影响HTTP / 1.0的设计。 1.1。 So, while it was after-the-fact in a lot of ways, REST is the design style behind HTTP. 因此,尽管在很多方面都是事后的事,但是REST是HTTP背后的设计风格。

Fielding's dissertation can be found at http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm and is very much worth reading, and also very readable. 菲尔丁的论文可以在http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm上找到,非常值得阅读,也很易读。 PhD dissertations can be pretty hard-going, but this one is wonderfully well-described and very readable to those of us without a comparable level of Computer Science. 博士学位论文可能非常艰辛,但是如果没有类似的计算机科学水平,那么这篇论文的描述就很好,并且对于我们中的人来说可读性很好。 It helps that REST itself is pretty simple; REST本身非常简单,这很有帮助。 it's one of those things that are obvious after someone else has come up with it. 这是别人提出来之后显而易见的事情之一。 (It also for that matter encapsulates a lot of things that older web developers learnt themselves the hard way in one simple style, which made reading it a major "a ha!" moment for many). (为此,它还封装了许多旧的Web开发人员以一种简单的方式以艰难的方式学习到的东西,这使得阅读它对许多人来说都是一个重要的时刻!)。

Other application-level protocols as well as HTTP can also use REST, but HTTP is the classic example. 其他应用程序级协议以及HTTP也可以使用REST,但是HTTP是经典示例。

Because HTTP uses REST, all uses of HTTP are using a REST system. 因为HTTP使用REST,所以HTTP的所有使用都在使用REST系统。 The description of a web application or service as RESTful or non-RESTful relates to whether it takes advantage of REST or works against it. 将Web应用程序或服务描述为RESTful或非RESTful涉及到它是利用REST还是对其进行工作。

The classic example of a RESTful system is a "plain" website without cookies (cookies aren't always counter to REST, but they can be): Client state is changed by the user clicking a link which loads another page, or doing GET form queries which brings results. RESTful系统的经典示例是没有cookie的“普通”网站(cookie并不总是与REST相反,但它们可以):用户通过单击加载另一个页面的链接或执行GET表单来更改客户端状态。带来结果的查询。 POST form queries can change both server and client state (the server does something on the basis of the POST, and then sends a hypertext document that describes the new state). POST表单查询可以更改服务器和客户端状态(服务器在POST的基础上执行某些操作,然后发送描述新状态的超文本文档)。 URIs describe resources, but the entity (document) describing it may differ according to content-type or language preferred by the user. URI描述资源,但是描述它的实体(文档)可能会根据用户喜欢的内容类型或语言而有所不同。 Finally, it's always been possible for browsers to update the page itself through PUT and DELETE though this has never been very common and if anything is less so now. 最后,浏览器始终可以通过PUT和DELETE来更新页面本身,尽管这种情况从未很普遍,现在已经不那么普遍了。

The classic example of a non-RESTful system using HTTP is something which treats HTTP as if it was a transport protocol, and with every request sends a POST of data to the same URI which is then acted upon in an RPC-like manner, possibly with the connection itself having shared state. 使用HTTP的非RESTful系统的经典示例是将HTTP视为一种传输协议,并且每次请求都将数据POST发送到同一URI,然后以类似RPC的方式对其进行处理连接本身具有共享状态。

A RESTful computer-readable (ie not a website in a browser, but something used programmatically) system would obtain information about the resources concerned by GETting URI which would then return a document (eg in XML, but not necessarily) which would describe the state of the resource, including URIs to related resources (hypermedia therefore), change their state through PUTting entities describing the new state or DELETEing them, and have other actions performed by POSTing. RESTful计算机可读(即不是浏览器中的网站,而是通过编程方式使用的某种东西)系统将通过GETURI获取有关资源的信息,然后URI将返回描述状态的文档(例如,但不一定是XML)包括相关资源的URI(因此为超媒体)的URI,通过描述新状态的PUTTING实体或删除它们来更改其状态,并通过POST执行其他操作。

Key advantages are: 主要优点是:

Scalability: The lack of shared state makes for a much more scalable system (demonstrated to me massively when I removed all use of session state from a heavily hit website, while I was expecting it to give a bit of extra performance, even a long-time anti-session advocate like myself was blown-away by the massive gain from removing what had been pretty slim use of sessions, it wasn't even why I had been removing them!) 可扩展性:缺乏共享状态使得系统的可扩展性大大提高(当我从遭受严重打击的网站上删除所有对会话状态的使用时,这在我身上得到了充分证明,而我希望它会带来一些额外的性能,甚至是更长的时间,像我这样的时间反会话倡导者被删除会话本来就很小的收获所震撼,这甚至都不是我为什么删除它们的原因!)

Simplicity: There are a few different ways in which REST is simpler than more RPC-like models, in particular there are only a few "verbs" that are ever possible, and each type of resource can be reasoned about in reasonable isolation to the others. 简单性:REST比其他类似RPC的模型有多种简单的方法,尤其是只有少数“动词”是可行的,每种资源都可以合理地与其他资源隔离开来。 。

Lightweight Entities: More RPC-like models tend to end up with a lot of data in the entities sent both ways just to reflect the RPC-like model. 轻量级实体:更多的类似RPC的模型倾向于最终以两种方式发送的实体中的大量数据只是为了反映类似RPC的模型。 This isn't needed. 不需要这个。 Indeed, sometimes a simple plain-text document is all that is really needed in a given case, in which case with REST, that's all we would need to send (though this would be an "end-result" case only, since plain-text doesn't link to related resources). 实际上,有时在给定情况下真正需要的只是一个简单的纯文本文档,在这种情况下,使用REST,这就是我们需要发送的所有文档(尽管这只是“最终结果”的情况,因为纯文本文档文字未链接到相关资源)。 Another classic example is a request to obtain an image file, RPC-like models generally have to wrap it in another format, and perhaps encode it in some way to let it sit within the parent format (eg if the RPC-like model uses XML, the image will need to be base-64'd or similar to fit into valid XML). 另一个经典示例是请求获取图像文件,类似RPC的模型通常必须将其包装为另一种格式,并可能以某种方式对其进行编码以使其位于父格式内(例如,如果类似RPC的模型使用XML) ,则该图片将需要使用base-64编码或类似图片,以适合有效的XML)。 A RESTful model would just transmit the file the same as it does to a browser. RESTful模型只会将文件传输到浏览器,就像传输文件一样。

Human Readable Results: Not necessarily so, but it is often easy to build a RESTful webservice where the results are relatively easy to read, which aids debugging and development no end. 人类可读的结果:不一定是这样,但是在结果相对易于阅读的地方构建RESTful Web服务通常很容易,这有助于调试和开发无止境。 I've even built one where an XSLT meant that the entire thing could be used by humans as a (relatively crude) website, though it wasn't primarily for human-use (essentially, the XSLT served as a client to present it to users, it wasn't even in the spec, just done to make my own development easier!). 我什至已经建立了一个XSLT,意思是整个东西都可以被人类用作一个(相对粗糙的)网站,尽管它并不是主要供人类使用(实质上,XSLT充当了将其呈现给客户的客户端)。用户,甚至没有在规范中,只是为了简化我自己的开发!)。

Looser binding between server and client: Leads to easier later development or moves in how the system is hosted. 服务器和客户端之间的绑定松动:导致以后的开发更容易,或者系统的托管方式发生了变化。 Indeed, if you keep to the hypertext model, you can change the entire structure, including moving from single-host to multiple hosts for different services, without changing client code at all. 的确,如果您保留超文本模型,则可以更改整个结构,包括从单主机迁移到用于不同服务的多个主机,而根本无需更改客户端代码。

Caching: For the GET operations where the client obtains information about the state of a resource, standard HTTP caching mechanisms allow both for statements that the resource won't meaningfully change until a certain date at the earliest (no need to query at all until then) or that it hasn't changed since the last query (send a couple hundred bytes of headers saying this rather than several kilobytes of data). 缓存:对于客户端获得有关资源状态信息的GET操作,标准的HTTP缓存机制允许两种声明,即最早直到某个日期资源才有意义地改变(直到那时才需要查询) ),或者自上次查询以来没有更改过(发送数百字节的标头说明了这一点,而不是几千字节的数据)。 The improvement in performance can be immense (big enough to move the performance of something from the point where it is impractical to use to the point where performance is no longer a concern, in some cases). 性能的提高可能是巨大的(在某些情况下,它的性能足以将某件产品的性能从不实用的位置转移到不再关注性能的位置)。

Availability of toolkits: Because it works at a relatively simple level, if you have a webserver you can build a RESTful system's server and if you have any sort of HTTP client API (XHR in browser javascript, HttpWebRequest in .NET, etc) you can build a RESTful system's client. 工具箱的可用性:因为它的工作相对简单,所以如果您有Web服务器,则可以构建RESTful系统的服务器,并且如果您具有任何HTTP客户端API(浏览器javascript中的XHR,.NET中的HttpWebRequest等),则可以构建一个RESTful系统的客户端。

Resiliance: In particular, the lack of shared state means that a client can die and come back into use without the server knowing, and even the server can die and come back into use without the client knowing. 弹性:特别是,缺少共享状态意味着客户端可以在服务器不知情的情况下死亡并重新使用,甚至服务器也可以在客户端不知情的情况下死亡并重新使用。 Obviously communications during that period will fail, but once the server is back online things can just continue as they were. 显然,在此期间的通信将失败,但是一旦服务器重新联机,一切就可以照原样继续进行。 This also really simplifies the use of web-farms for redundancy and performance - each server acts like it's the only server there is, and it doesn't matter that its actually only dealing with a fraction of the requests from a given client. 这也确实简化了网络服务器场的冗余和性能使用-每个服务器都像它是唯一的服务器一样工作,并且它实际上只处理来自给定客户端的部分请求也没关系。

REST is an approach that leverages the HTTP protocol, and is not an alternative to it. REST是一种利用HTTP协议的方法,而不是替代方法。

Data is uniquely referenced by URL and can be acted upon using HTTP operations (GET, PUT, POST, DELETE, etc). 数据由URL唯一引用,并且可以使用HTTP操作(GET,PUT,POST,DELETE等)进行操作。 A wide variety of mime types are supported for the message/response but XML and JSON are the most common. 消息/响应支持多种mime类型,但是XML和JSON是最常见的。

For example to read data about a customer you could use an HTTP get operation with the URL http://www.example.com/customers/1 . 例如,要读取有关客户的数据,您可以对URL http://www.example.com/customers/1使用HTTP get操作。 If you want to delete that customer, simply use the HTTP delete operation with the same URL. 如果要删除该客户,只需对相同的URL使用HTTP删除操作。

The Java code below demonstrates how to make a REST call over the HTTP protocol: 以下Java代码演示了如何通过HTTP协议进行REST调用:

String uri =
    "http://www.example.com/customers/1";
URL url = new URL(uri);
HttpURLConnection connection =
    (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");

JAXBContext jc = JAXBContext.newInstance(Customer.class);
InputStream xml = connection.getInputStream();
Customer customer =
    (Customer) jc.createUnmarshaller().unmarshal(xml);

connection.disconnect();

For a Java (JAX-RS) example see: 对于Java(JAX-RS)示例,请参见:

REST is not a protocol, it is a generalized architecture for describing a stateless, caching client-server distributed-media platform. REST不是协议,它是一种通用的体系结构,用于描述无状态的缓存客户端-服务器分布式媒体平台。 A REST architecture can be implemented using a number of different communication protocols, though HTTP is by far the most common. REST体系结构可以使用多种不同的通信协议来实现,尽管HTTP是迄今为止最常见的协议。

REST is not a protocol, it is a way of exposing your application, mostly done over HTTP. REST不是协议,它是公开应用程序的一种方式,通常是通过HTTP完成的。

for example, you want to expose an api of your application that does getClientById 例如,您要公开应用程序中执行getClientById的api
instead of creating a URL 而不是创建一个URL

yourapi.com/getClientById?id=4 yourapi.com/getClientById?id=4
you can do 你可以做
yourapi.com/clients/id/4 yourapi.com/clients/id/4

since you are using a GET method it means that you want to GET data 由于您使用的是GET方法,这意味着您要获取数据

You take advantage over the HTTP methods: GET/DELETE/PUT 您可以利用HTTP方法的优势:GET / DELETE / PUT
yourapi.com/clients/id/4 can also deal with delete, if you send a delete method and not GET, meaning that you want to dekete the record yourapi.com/clients/id/4也可以处理删除,如果您发送的是delete方法而不是GET,则意味着您想删除记录

All the answers are good. 所有的答案都很好。

I hereby add a detailed description of REST and how it uses HTTP . 我在此添加对REST及其如何使用HTTP的详细描述。

REST = Representational State Transfer REST =代表性状态转移

REST is a set of rules, that when followed, enable you to build a distributed application that has a specific set of desirable constraints. REST是一组规则,遵循这些规则可使您构建具有一组特定的期望约束的分布式应用程序。

It is stateless, which means that ideally no connection should be maintained between the client and server. 它是无状态的,这意味着在理想情况下,客户端和服务器之间不应保持任何连接。

It is the responsibility of the client to pass its context to the server and then the server can store this context to process the client's further request. 客户端负责将其上下文传递给服务器,然后服务器可以存储此上下文以处理客户端的进一步请求。 For example, session maintained by server is identified by session identifier passed by the client. 例如,服务器维护的会话由客户端传递的会话标识符标识。

Advantages of Statelessness: 无国籍的优势:

  1. Web Services can treat each method calls separately. Web服务可以分别处理每个方法调用。
  2. Web Services need not maintain the client's previous interaction. Web服务不需要维护客户端的先前交互。
  3. This in turn simplifies application design. 反过来,这简化了应用程序设计。
  4. HTTP is itself a stateless protocol unlike TCP and thus RESTful Web Services work seamlessly with the HTTP protocols. HTTP本身是不同于TCP的无状态协议,因此RESTful Web服务可与HTTP协议无缝地协同工作。

Disadvantages of Statelessness: 无国籍的缺点:

  1. One extra layer in the form of heading needs to be added to every request to preserve the client's state. 需要将每个标题的形式增加一层,以保留客户端的状态。
  2. For security we may need to add a header info to every request. 为了安全起见,我们可能需要在每个请求中添加标头信息。

HTTP Methods supported by REST: REST支持的HTTP方法:

GET: /string/someotherstring : GET: /string/someotherstring
It is idempotent(means multiple calls should return the same results every time) and should ideally return the same results every time a call is made 它是幂等的(意味着多个调用每次应返回相同的结果),理想情况下每次调用应返回相同的结果

PUT: 放:
Same like GET. 像GET一样。 Idempotent and is used to update resources. 幂等,用于更新资源。

POST: should contain a url and body POST:应包含网址和正文
Used for creating resources. 用于创建资源。 Multiple calls should ideally return different results and should create multiple products. 理想情况下,多次通话应返回不同的结果,并应创建多种产品。

DELETE: 删除:
Used to delete resources on the server. 用于删除服务器上的资源。

HEAD: 头:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. HEAD方法与GET相同,除了服务器在响应中不得返回消息正文。 The meta information contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. 响应HEAD请求的HTTP头中包含的元信息应该与响应GET请求发送的信息相同。

OPTIONS: 选项:

This method allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval. 此方法允许客户端确定与资源相关联的选项和/或要求,或者服务器的功能,而无需暗示资源操作或启动资源检索。

HTTP Responses HTTP响应

Go here for all the responses . 去这里获得所有答复

Here are a few important ones: 以下是一些重要的方面:
200 - OK 200-好
3XX - Additional information needed from the client and url redirection 3XX-客户端和URL重定向所需的其他信息
400 - Bad request 400-错误的要求
401 - Unauthorized to access 401-未经授权的访问
403 - Forbidden 403-禁止
The request was valid, but the server is refusing action. 该请求有效,但是服务器拒绝操作。 The user might not have the necessary permissions for a resource, or may need an account of some sort. 用户可能没有资源的必要权限,或者可能需要某种帐户。

404 - Not Found 404-找不到
The requested resource could not be found but may be available in the future. 找不到请求的资源,但将来可能可用。 Subsequent requests by the client are permissible. 客户的后续请求是允许的。

405 - Method Not Allowed A request method is not supported for the requested resource; 405-不允许使用的方法请求的资源不支持请求方法。 for example, a GET request on a form that requires data to be presented via POST, or a PUT request on a read-only resource. 例如,要求通过POST呈现数据的表单上的GET请求,或只读资源上的PUT请求。

404 - Request not found 404-找不到请求
500 - Internal Server Failure 500-内部服务器故障
502 - Bad Gateway Error 502-错误的网关错误

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

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