[Computer network] REST Interface

1 REST

REST is a style of software architecture, if your interface is a REST interface, then the interface can be considered as REST-style.
REST interface is built around 资源deployment, HTTP, URLnamely resources, using the HTTP protocol, this can actually rest and HTTP has nothing to do, but now we commonly use are relying on REST HTTP protocol.

2 URI syntax

URI = scheme “://” authority “/” path [ “?” query ][ “#” fragment ]

scheme: refers to the use of the underlying protocol, such as HTTP, HTTPS, FTP
Host: the IP address or domain name server
port: the port, the default HTTP 80
path: the path to access the resource, the route is route we various web frame defined
query: is sent to the server parameters
fragment: anchor, navigate to the resource page, anchor the resource id

3 HTTP verb

URL for a particular resource type of operation, represented by the HTTP verbs.
Common HTTP verbs are the following five (in brackets is the corresponding SQL commands).

      GET(SELECT):从服务器取出资源(一项或多项)。
      POST(CREATE):在服务器新建一个资源。
      PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源)。
      PATCH(UPDATE):在服务器更新资源(客户端提供改变的属性)。
      DELETE(DELETE):从服务器删除资源。
4 Resources filter

If you only need to query data API certain conditions should provide parameters, filtering results are returned.
Here are some common parameters:

    ?limit=10:指定返回记录的数量
    ?offset=10:指定返回记录的开始位置。
    ?page=2&per_page=100:指定第几页,以及每页的记录数。
    ?sortby=name&order=asc:指定返回结果按照哪个属性排序,以及排序顺序。
    ?teacher_subject=语文:指定筛选条件
5 return

There are many server returns a status code has been set to 200, and then return some of the state body inside a custom code to indicate that the server returns the results of the state code.
Since the HTTP protocol REST API is used directly, it is also possible to use the status code of the HTTP protocol status code.

200 OK 服务器返回用户请求的数据,该操作是幂等的
201 CREATED 新建或者修改数据成功
204 NOT CONTENT 删除数据成功
400 BAD REQUEST 用户发出的请求有问题,该操作是幂等的
401 Unauthoried 表示用户没有认证,无法进行操作
403 Forbidden 用户访问是被禁止的
422 Unprocesable Entity 当创建一个对象时,发生一个验证错误
500 INTERNAL SERVER ERROR 服务器内部错误,用户将无法判断发出的请求是否成功
503 Service Unavailable 服务不可用状态,多半是因为服务器问题,例如CPU占用率大,等等
......
Published 15 original articles · won praise 1 · views 1649

Guess you like

Origin blog.csdn.net/weixin_43465579/article/details/104505801