REST and HTTP request

Representational State Transfer (REST)

In practice, REST is implemented with HTTP and XML or JSON.

While all HTTP methods might be used by a RESTful web service, the ones that are really important are GET, PUT, POST, DELETE, and HEAD.

REST and HTTP request_第1张图片
image.png

GET

GET is used to retrieve a representation (or, "stuff") from a resource. GET is widely used today for downloading web pages. However, it might be used to GET any kind whatever representation a resource may provide, including XML data.

从一个具体的resource 获取数据
额外的参数信息都在url里面
我们可以bookmark GET请求
我们应该只在获取信息的时候使用GET

HEAD

head是获取资源,是安全的
HEAD is similar to GET. The significant difference is that the representation is not returned. Rather, only the HTTP header with the various headers is returned. The HTTP protocol has a number of headers, such as Content-Length and the date the resource was last modified. Sometimes, it is useful to use HEAD to see if something has changed before deciding down transfer a large representation over the network.

PUT

put 是修改,是safe的
PUT is used to store a representation at a resource. That sounds strange, but said another way, this is a way to perhaps upload a file and have it stored at a particular URI at the web server or to perhaps create a user account on a web forum. Imagine, if you will, creating a new blog entry that has some files attached. PUT would be the appropriate method to use to upload those files, especially if the client (or "user agent") knows exactly where those files should be placed. If one issues a PUT two times, then the second representation overwrites the first representation. This is entirely harmless if a file is uploaded two times, since the end result is the same file. However, if two different users issue a PUT to the same URI, the representation stored at a given URI will be the last one received.

DELETE

delete是删除,是安全的
DELETE, as you can imagine, deletes information that one stores. If there is a resource that one would like to remove from the server, DELETE is the method one should use.

POST

post是提交新资源,是不安全的
POST is similar to PUT, and therefore confusing. POST also creates a resource, like PUT. The key difference is that POST is used when the server is in control of storing information, not the client. This is usually the case when posting a blog entry, for example. If the client wishes to create a new blog entry, it is the server that is responsible for storing the information in a database and assigning a unique value and/or URI to the newly posted content.

POST is also the appropriate tool to use when the operation does not result in the creation of a resource, but perhaps the invocation of some action. As an example, using a web-based form to transmit an email message would be an appropriate use of POST.

OK, that last sentence will raise a few eyebrows in the REST community. But, alas, what system exists where there are not some "things" that do something, other than store information? If one wishes to transmit an e-mail message, for example, POST is the method to use. In that case, some action is performed, but a new resource is not created as a consequence.

Safety and Idempotence

There are two other terms associated with REST that you should know, simply because they appear in all of the literate related to REST. Those are the words "safe" and "idempotent". These words do not come from Fielding's original PhD thesis on REST, but do appear in RFC 2616 and have been popularized through the book [RESTuses GET in such a way as to modify any state contained within a resource, then you have violated the rules.

Note:因为get是safe的,所以get的结果通常可以用来缓存。

References:

https://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers?rq=1

  • https://www.packetizer.com/ws/rest.html

你可能感兴趣的:(REST and HTTP request)