REST design style

Restful

  • Features of REST:
  • figurative. Generally refers to the presentation layer, the object to be presented is the resource. For example, when a client accesses a server, the data obtained is a resource. Such as text, pictures, audio and video, etc.

  • Representation: The representation of the resource. txt format, html format, json format, jpg format, etc. The browser determines the location of the resource through the URL, but it needs to be specified in the HTTP request header with the Accept and Content-Type fields, which are descriptions of the resource performance.

  • State transitions: The process by which the client and server interact. In this process, there must be data and state transformation, which is called state transformation. Among them, GET means to obtain resources, POST means to create new resources, PUT means to update resources, and DELETE means to delete resources. These four modes of operation are the most commonly used in the HTTP protocol.

    • RESTful architecture:
    • Each URL represents a resource;
    • Between the client and the server, some kind of presentation layer of this resource is passed;
    • The client operates the server resources through four http verbs to realize the state transition of the presentation layer.

How to design a RESTful API:

1. Domain Name:

Deploy the api under a dedicated domain name:

http://api.example.com

Or put the api under the main domain name:

http://www.example.com/api/

2. Version:

Put the version number of the API in the url.

http://www.example.com/app/1.0/info
http://www.example.com/app/1.2/info

3. Path:

The path represents the specific URL of the API. Each URL represents a resource. A resource is used as a URL. The URL cannot contain verbs but only nouns. The general nouns should correspond to the table names of the database. And nouns must be plural.

Error example:

http://www.example.com/getGoods
http://www.example.com/listOrders

Correct example:

#获取单个商品
http://www.example.com/app/goods/1
#获取所有商品
http://www.example.com/app/goods

Fourth, use standard HTTP methods:

For specific types of operations on resources, represented by HTTP verbs. There are four commonly used HTTP verbs.

GET     SELECT :从服务器获取资源。
POST    CREATE :在服务器新建资源。
PUT     UPDATE :在服务器更新资源。
DELETE  DELETE :从服务器删除资源。

Example:

#获取指定商品的信息
GET http://www.example.com/goods/ID
#新建商品的信息
POST http://www.example.com/goods
#更新指定商品的信息
PUT http://www.example.com/goods/ID
#删除指定商品的信息
DELETE http://www.example.com/goods/ID

5. Filter information:

If there is a lot of resource data, the server cannot return all the data to the client at one time. The API should provide parameters to filter the returned results. Example:

#指定返回数据的数量
http://www.example.com/goods?limit=10
#指定返回数据的开始位置
http://www.example.com/goods?offset=10
#指定第几页,以及每页数据的数量
http://www.example.com/goods?page=2&per_page=20

6. Status code:

The status code and prompt information returned by the server to the user are commonly used:

200 OK  :服务器成功返回用户请求的数据
201 CREATED :用户新建或修改数据成功。
202 Accepted:表示请求已进入后台排队。
400 INVALID REQUEST :用户发出的请求有错误。
401 Unauthorized :用户没有权限。
403 Forbidden :访问被禁止。
404 NOT FOUND :请求针对的是不存在的记录。
406 Not Acceptable :用户请求的的格式不正确。
500 INTERNAL SERVER ERROR :服务器发生错误。

7. Error message:

Generally speaking, the error information returned by the server is returned in the form of a key-value pair.

{
    error:'Invalid API KEY'
}

8. Response result:

For different results, the results returned by the server to the client should conform to the following specifications.

#返回商品列表
GET    http://www.example.com/goods
#返回单个商品
GET    http://www.example.com/goods/cup
#返回新生成的商品
POST   http://www.example.com/goods
#返回一个空文档
DELETE http://www.example.com/goods

9. Use links to associate related resources:

Provides a method of linking to other APIs when returning the response result, so that the client can easily obtain the related information.

10. Others:

The data format returned by the server should use JSON as much as possible and avoid using XML.

Guess you like

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