What specifications should a Restful API follow? Advanced Python

  Restful API is a relatively mature set of API design concepts for Internet applications. Rest is a set of architectural constraints and principles. How to construct Restful constraints and principles is called Restful architecture. Restful architecture has a clear structure and conforms to Standard, easy to understand and easy to expand, etc., are used by more and more websites!

  The Restful API interface specification includes the following parts:

   1. Agreement

  The communication protocol between the API and the user always uses the HTTPs protocol.

   2. Domain Name

  The API should be deployed under a dedicated domain name as much as possible, such as https://api.oldboyedu.com; if it is determined that the API is very simple and there will be no further expansion, it can be considered under the main domain name, such as https://oldboyedu.com /api/.

   3. Version

  You can put the version number in the HTTP header or in the URL, such as https://api.oldboyedu.com/v1/

   4. Path

  A path is an address, which is represented as a web address on the Internet. In the RESTful architecture, each web address represents a resource, so there can be no verbs in the web address, only nouns, and the nouns used are often the same as the database tables. corresponding to the name. Generally speaking, the tables in the database are all "collections" of the same kind of records, so the nouns in the API should also use plurals, such as https://api.oldboyedu.com/v1/students.

   5. HTTP verbs

  The specific operation types of resources are represented by HTTP verbs. There are mainly the following types of HTTP verbs, and the corresponding SQL commands are in parentheses.

  1. GET(SELECT): Get the resource (one or more) from the server;

  2. POST(CREATE): Create a new resource on the server;

  3. PUT (UPDATE): Update the resource on the server (the client provides the complete resource after the change);

  4. PATCH(UPDATE): Update resources on the server (the client provides the changed attributes);

  5. DELETE(DELETE): delete the resource from the server;

  6. HEAD: Get the metadata of the resource;

  7. OPTIONS: Get information about which properties of the resource can be changed by the client.

   6. Filter information

  If the number of records is large, it is impossible for the server to return them to the user. The API will provide parameters to filter the returned results. Common parameters are:

  1. ?limit=20: Specify the number of returned records to be 20;

  2. ?offset=8: Specify the starting position of the returned record as 8;

  3. ?page=1&per_page=50: Specify page 1, and the number of records per page is 50;

  4. ?sortby=name&order=asc: Specifies that the returned results are sorted in ascending order according to the name attribute;

  5. ?animal_type_id=2: Specify filter conditions.

   7. Status code

  The server will return a status code and prompt information to the user. The following are some commonly used status codes:

  1. 200 OK - [GET]: The server successfully returned the data requested by the user;

  2. 201 CREATED - [POST/PUT/PATCH]: User created or modified data successfully;

  3. 202 Accepted - [*]: Indicates that a request has entered the background queue (asynchronous task);

  4. 204 NO CONTENT - [DELETE]: User successfully deleted data;

  5. 400 INVALID REQUEST - [POST/PUT/PATCH]: There is an error in the request sent by the user, and the server does not create or modify data;

  6. 401 Unauthorized - [*]: Indicates that the user does not have permission (token, username, password error);

  7. 403 Forbidden - [*] indicates that the user is authorized (as opposed to 401 error), but access is forbidden;

  8. 404 NOT FOUND - [*]: The request sent by the user is for a record that does not exist, and the server does not operate;

  9. 406 Not Acceptable - [GET]: The format requested by the user is not available;

  10. 410 Gone -[GET]: The resource requested by the user is permanently deleted and will not be available again;

  11. 422 Unprocesable entity - [POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误;

  12. 500 INTERNAL SERVER ERROR - [*]:服务器发生错误,用户将无法判断发出的请求是否成功。

   八、错误处理

  如果状态码是4xx,就会向用户返回出错信息,一般来说,返回的信息中将error作为键名,出错信息作为键值。

   九、返回结果

  针对不同操作,服务器向用户返回的结果应该符合以下规范:

  1. GET /collection:返回资源对象的列表(数组);

  2. GET /collection/resource:返回单个资源对象;

  3. POST /collection:返回新生成的资源对象;

  4. PUT /collection/resource:返回完整的资源对象;

  5. PATCH /collection/resource:返回完整的资源对象;

  6. DELETE /collection/resource:返回一个空文档。

   十、Hypermedia API

  RESTful API最好做到Hypermedia,即返回结果中提供链接,连向其他API方法,使得用户不查文档,也知道下一步应该做什么。

  以上是Restful API设计应遵循的十大规范,除此之外,Restful API还需注意身份认证应该使用OAuth 2.0框架,服务器返回的数据格式,应该尽量使用JSON,避免使用XML。

  推荐看下老男孩教育的课程,Python开发自2012年开始至今,课程体系已成熟,且目前开设有0基础可以学习的全日制Python全栈开发脱产班和在职运维、测试、前端开发等朋友们可以学习的Python自动化开发周末班的课程。现在Python全栈开发开始实行“双轨制5+5”的学习模式,课程增加了人工智能(图像识别、无人机、无人驾驶等)以及go语言的相关知识,以拓展大家的知识领域。

Guess you like

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