Fast learning -RESTFul design

Two, RESTFul design

1, Rest and RestFul

REST (English: Representational State Transfer, referred to as REST), Representational State Transfer, refers to a set of architectural principles.

Restful: comply with the web service or web application of the principle of rest.

2, API Six Elements

Resource path (URI), HTTP verb (Method), filter information (query-string), a status code (Status-code), the error message (Error), returns the result (the Result)

Here Insert Picture Description

1) Resource URI path

Resources: All data stored on the server (such as: music / video / article / personal information ...). All server-side resources (project resources are usually referring to a data table data)

URI (Uniform Resource Identifier): Uniform Resource Identifier, contains the URL and URN.

URL (Uniform Resource Locator): Uniform Resource Locator

URN (Uniform Resource Name): Uniform Resource Naming

In the HTTP protocol, the following composition URI

Schema://host[:port]/path[?query-string]

Schema: the protocol type to use, such as http / https / ftp etc.

host: host domain name or IP

port: Port number (optional)

path: the path

query-string: query parameters (optional)

example:

http://www.tpshop.com/users

https://www.tpshop.com:8080/users?id=100

2) HTTP verbs (request mode)

For resource, are generally four operations, CURD (add / delete / change / check)

GET: access to resources from the server (one or more)

POST: Create a new resource on the server

PUT: update the resource on the server, the server returns a complete property

DELETE: delete a resource from the server

3) filter information

Often called the request or query string parameters.

4) Response Status Code

Service information returned from the server to tell the client operating results.

Common status codes:

status code meaning Explanation
200 OK The success of the operation and returns data
201 CREATED New success
204 NO CONTENT successfully deleted
400 BAD REQUEST Request Syntax Error
403 Forbidden Request resources without permission
404 NOT FOUND I did not find the requested resource

GET

200 (OK) - represents the response has been issued in
204 (content) - represents available resources
301 (Moved Permanently) - URI resource has been updated
303 (See Other) - Other (e.g., load balancing)
304 (Not Modified ) - resource not changed (cache)
400 (bad request) - refer to bad request (such as parameter error)
404 (not found) - the resource does not exist
406 (not acceptable) - the server does not support the required representation
500 (internal server error) - generic error response
503 (service Unavailable) - the server is currently unable to handle the request

POST
200 (the OK) - If the existing resource has been changed
201 (created) - If a new resource is created
202 (accepted) - to process the request has been accepted but not completed (asynchronous processing)
301 (Moved Permanently) - URI of the resource is updated
303 (See other) - other (eg, load balancing)
400 (bad request) - refer bad request
404 (not found) - the resource does not exist
406 (not acceptable) - the server does not support the required representation
409 (conflict) - conflict General
412 (precondition failed) - precondition failed (e.g., update conflicts when the execution condition)
415 (unsupported Media type) - received indicates unsupported
500 (internal server error) - generic error response
503 (Service Unavailable) - service is currently unable to handle the request

PUT
200 (the OK) - If there is a resource was changed
201 (created) - If a new resource is created
301 (Moved Permanently) - URI resource has changed
303 (See Other) - Other (eg, load balancing)
400 (Bad Request ) - refers bad request
404 (not found) - resource does not exist
406 (not acceptable) - the server does not support the required representation
409 (conflict) - universal conflict
412 (precondition failed) - precondition failed (e.g., the update execution condition conflicts of)
415 (unsupported Media of the type) - received representation is not supported
500 (internal server error) - generic error response
503 (service Unavailable) - currently unable to handle service requests

DELETE
200 (the OK) - Resource has been deleted
301 (Moved Permanently) - URI resource has changed
303 (See Other) - Other, such as load balancing
400 (bad request) - refer Bad Request
404 (not found) - the resource is not the presence of
409 (conflict) - general conflict
500 (internal server error) - generic error response

503 (Service Unavailable) - the server is currently unable to handle the request

5) Error Messages

If the status code 4xx or 5xx, we need to tell the error message corresponding to the client usually returns to Json format:

{

"Error": "error"

}

6) return result

For different operations, the results need to return to service should meet these specifications

GET / collections - returns a resource list (array)

GET / collections /: id - returns a single resource eg / collections / 1.

POST / collections - a new generation of resources returned

PUT / collections /: id - returns the full resource properties

DELETE / collections /: id - returns a status code 204 + empty document

Actual development, generally sets a status code, error message, return data are placed in the returned results.

such as

{"code":200, 'msg':'success', "data":{"id":1,"goods_name":"tp"}}

3, RestFul interface design style

RESTFul is a software design, mainly used in software client and server interaction.

RESTFul is the most popular API design specifications for the design of Web data interface.

Example RESTFul style data interface:

To news resources, for example: URI and HTTP verbs designed as follows

HTTP verb URI Michi径 Explanation
GET http: // domain / news Gets a list of data
GET http: // domain / news /: id According to a data acquisition id
POST http: // domain / news Adding a Data
PUT http: // domain / news /: id Id data according to a modified
DELETE http: // domain / news /: id To delete a data according to id
Released 1842 original articles · won praise 1964 · Views 170,000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/105122214