In 2000, Roy Thomas Fielding, Ph.D., in his PhD thesis Architectural Styles and the Design of Network-based Software Architectures have proposed several architectural styles for software applications, and REST is outlined in this paper as one of them.
REST: An acronym for Representational State Transfer. This is generally interpreted as “presentation layer state transitions”.
REST is a design style, not a standard. The interaction between a client and a server. What we need to focus on is how to design restful web interfaces.
-
REST features:
-
The representational. Generally refers to the performance layer, to the performance of the object is the resource. For example, when a client accesses a server, the data it retrieves is a resource. Such as text, pictures, audio and video.
-
Representation: The representation of resources. TXT format, HTML format, JSON format, JPG format. 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 describe how the resource will behave.
-
State transition: The process of client and server interaction. In this process, there must be data and state transformation, which is called state transition. GET: obtains resources, POST: creates resources, PUT: updates resources, and DELETE: deletes resources. These four operations are most commonly used in HTTP.
- A RESTful architecture:
- Each URL represents a resource;
- Some representation layer that passes this resource between the client and the server;
- The client operates on the server resources through four HTTP verbs to achieve state transformation at the presentation layer.
How to design RESTful apis:
I. Domain name:
Deploying the API under a private domain name:
http://api.example.com
Copy the code
Or put the API under the main domain:
http://www.example.com/api/
Copy the code
{# ii, 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
Copy the code
Iii. Path:
The path represents the specific URL of the API. Each web address represents a resource. Resources as a website, the website can not have a verb can only have a noun, general nouns should be corresponding to the database table name. And use plural nouns.
Examples of errors:
http://www.example.com/getGoods
http://www.example.com/listOrders
Copy the code
Correct examples:
# for a single http://www.example.com/app/goods/1 http://www.example.com/app/goods # for all goodsCopy the code
Use the standard HTTP method.
The specific types of operations on resources are represented by HTTP verbs. There are four commonly used HTTP verbs.
GET SELECT: obtains resources from the server. POST CREATE: Creates a resource on the server. PUT UPDATE: Updates resources on the server. DELETE DELETE: Deletes resources from the server.Copy the code
Example:
# # for specified commodity information GET http://www.example.com/goods/ID new commodity information POST PUT http://www.example.com/goods # update assigned commodity information http://www.example.com/goods/ID # DELETE specified commodity information DELETE http://www.example.com/goods/IDCopy the code
Five, filtering information:
If there is a large amount of resource data, the server cannot return all data to the client at a time. The API should provide parameters that filter the return results. Example:
# specifies the amount of data returned http://www.example.com/goods?limit=10 # the start location http://www.example.com/goods?offset=10 # the specified return data to the specified page Numbers, The amount of data, and each page at http://www.example.com/goods?page=2&per_page=20Copy the code
{# 6, status code:}
Status codes and prompts returned by the server to users. The following are commonly used:
200 OK: The server returns the requested data successfully. 201 CREATED: The user creates or modifies data successfully. 202 Accepted: Indicates that the request is queued in the background. 400 INVALID REQUEST: There is an error in the REQUEST sent by the user. 401 Unauthorized: The user does not have permission. 403 Forbidden: Access is Forbidden. 404 NOT FOUND: The request is for a record that does NOT exist. 406 Not Acceptable: The format requested by the user is Not correct. 500 INTERNAL SERVER ERROR: An ERROR occurs on the SERVER.Copy the code
{# 7, error message:}
Typically, the server returns error messages in the form of key-value pairs.
{
error: 'Invalid API KEY'
}
Copy the code
{# 8, response results:}
The results returned by the server to the client should conform to the following specifications for different results.
# list of return goods GET http://www.example.com/goods # returns a single commodity GET http://www.example.com/goods/cup # returns the newly generated POST goods The DELETE http://www.example.com/goods http://www.example.com/goods # returns an empty documentCopy the code
9. Use links to associate related resources:
Provide a method to link to other apis when the response result is returned, so that the client can easily obtain the associated information.
X. Others:
The data format returned by the server should be JSON rather than XML.