RESTful architecture

 

Name: REST, short for Representational State Transfer. My translation of this phrase is "presentation layer state transformation". In the name of REST, the subject is omitted, and "presentation layer" actually refers to the "presentation layer" of "Resources". If an architecture conforms to REST principles, it is called a RESTful architecture.

 

Resource: It is an entity on the network, or a specific information on the network. Each resource corresponds to a specific URI. To get this resource, just visit its URI.

 

Presentation layer: "Resource" is a kind of information entity, which can have various external manifestations. The form in which we specifically present the "resource" is called its "representation layer" (Representation). For example, text can be expressed in txt format, HTML format, XML format, JSON format, or even binary format; pictures can be expressed in JPG format or PNG format.

 

State Transfer: Visiting a website represents an interactive process between the client and the server. In this process, data and state changes are bound to be involved. The Internet communication protocol HTTP protocol is a stateless protocol. This means, all state is kept on the server side. Therefore, if the client wants to operate the server, it must use some means to make the "State Transfer" happen on the server side. And this transformation is based on the presentation layer, so it is "presentation layer state transformation". The means used by the client can only be the HTTP protocol. Specifically, in the HTTP protocol, there are four verbs that express the operation mode: GET, POST, PUT, DELETE. They correspond to four basic operations: GET is used to obtain resources, POST is used to create new resources (and can also be used to update resources), PUT is used to update resources, and DELETE is used to delete resources.

 

RESTful architecture:

(1) Each URI represents a resource;

(2) Between the client and the server, a certain presentation layer of this resource is transferred;

(3) The client operates the server-side resources through four HTTP verbs to achieve "presentation layer state transformation".

 

The wrong URI is:

  POST /accounts/1/transfer/500/to/2

The correct way to write it is to change the verb transfer to the noun transaction. The resource cannot be a verb, but can be a service:

  POST /transaction HTTP/1.1
  Host: 127.0.0.1
  
  from=1&to=2&amount=500.00

Another design misunderstanding is adding a version number to the URI :

  http://www.example.com/app/1.0/foo

  http://www.example.com/app/1.1/foo

  http://www.example.com/app/2.0/foo

Because different versions can be understood as different representations of the same resource, the same URI should be used. Version numbers can be distinguished in the Accept field of the HTTP request header (see Versioning REST Services ):

  Accept: vnd.example-com.foo+json; version=1.0

  Accept: vnd.example-com.foo+json; version=1.1

  Accept: vnd.example-com.foo+json; version=2.0

 

 

Protocol: https

SSL/TLS  (1) All information is encrypted and transmitted , and third parties cannot eavesdrop.

(2) It has a verification mechanism . Once it is tampered with, both parties in the communication will find it immediately.

(3) 配备身份证书,防止身份被冒充。

基本过程是这样的:

(1) 客户端向服务器端索要并验证公钥。

(2) 双方协商生成"对话密钥"。

(3) 双方采用"对话密钥"进行加密通信。

 

 

路径(Endpoint)

  • https://api.example.com/v1/zoos

 

 

参考文档 阮一峰 

http://www.ruanyifeng.com/blog/2011/09/restful.html

http://www.ruanyifeng.com/blog/2014/05/restful_api.html

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326008700&siteId=291194637