5.4 Restful
2000, Dr. Roy Thomas fielding in his doctoral dissertation architectural Styles and the Design of network-based software architectures Several architectural styles of software applications are presented, and rest as an architectural style is presented in this paper.
rest:representational State transfer, translation: "Representational status Transmission". It is generally interpreted as "representation state transition".
Rest is a design style, not a standard . Refers to the interaction between the client and the server. What we need to focus on is how to design a restful network interface.
- Features of rest:
figurative . Generally refers to the presentation layer, to show the object is the resources. For example, a client Access server, the data obtained is a resource. such as text, pictures, audio and video.
performance : the manifestation of resources. 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 of client and server interaction. In this process, there must be data and state conversions, which are called state transitions. Where get means to get a resource, post represents a new resource, put represents an update resource, and delete represents a delete resource. The most common of the HTTP protocols is the four modes of operation.
- RESTful architecture:
- Each URL represents a resource;
- Between the client and the server, a presentation layer that transmits such resources;
- The client uses four HTTP verbs to manipulate the server resources to realize the state transition of the representation layer.
How to design a restful API: first, domain name:
Deploy the API under a dedicated domain name:
Http://api.example.com
Or put the API under the primary domain name:
http://www.example.com/api/
Second, version:
Place the version number of the API in the URL.
http://www.example.com/app/1.0/infohttp://www.example.com/app/1.2/info
Third, Path:
The path represents the specific URL of the API. Each URL represents a resource. Resources as URLs, the URL can not have verbs can only have nouns, the general noun to the database table name corresponding. and nouns need to use plural.
Error Example:
http://www.example.com/getgoodshttp://www.example.com/listorders
Correct example:
# get a single item HTTP://WWW.EXAMPLE.COM/APP/GOODS/1# get all products http://www.example.com/app/goods
Iv. using the standard HTTP method:
For the specific operation type of the resource, it is represented by an HTTP verb. There are four commonly used HTTP verbs.
Get SELECT: Gets the resource from the server. POST Create: Creates a new resource on the server. PUT Update: Updates the resource on the server. Delete delete : Deletes the resource from the server.
Example:
# get information about a specified item GET http://www.example.com/goods/ID# New Item information POST http://www.example.com/goods # update information for a specified item PUT http://www.example.com/goods/ID# Delete information for the specified item delete Http://www.example.com/goods/ID
Five, filter information:
If there is more resource data, the server cannot return all data to the client at once. The API should provide parameters to filter the returned results. Instance:
# Specifies the number of returned data http://www.example.com/goods?limit=10# Specifies the start position of the returned data http://www.example.com/ goods?offset=10# Specifies the number of pages, and the amount of data per page http://www.example.com/goods?page=2&per_page=20
Six, status code:
The status code and prompt information returned by the server to the user, commonly used are:
OK : The server successfully returns the data requested by the user 201 CREATED: The user created or modified the data successfully. 202 Accepted: Indicates that the request has entered the background queue. INVALID Request: There was an error with the user. 401 Unauthorized: The user does not have permissions. 403 Forbidden: Access is forbidden. 404 Not FOUND: The request is for a record that does not exist. 406 Not acceptable: the user requested an incorrect format. INTERNAL Server Error: The server has errors.
VII. error message:
In general, the error message returned by the server is returned as a key-value pair.
{ error:'Invalid API KEY'}
Viii. Response Results:
The results returned by the server to the client should conform to the following specifications for different results.
# back to Product list Get http://www.example.com/goods# Returns a single item GET http://www.example.com/goods/ Cup # return the newly generated product POST http://www.example.com/goods# Returns an empty document DELETE Http://www.example.com/goods
IX. resources related to using link associations:
Provides a way to link other APIs when the response results are returned, making it convenient for the client to obtain associated information.
Ten, Other:
The data format returned by the server should use JSON as much as possible and avoid using XML.
Flask's RESTful