What interfaces are? ----- interfaces equivalent channel access to resources
Explanation: the interface is a channel, data transmission front end to a back end, the channel needs to be transmitted, the rear end of the check, analysis, and then returns a response, the front end receiving the response, the response content is determined, and finally displayed at a front end;
An interface is equivalent to a function to call the back end, and the function will return the value as a response to his message back to the client;
Corresponds to the client, a browser, a request sent by APP;
A request
Process must be requested by a request to the url
1.URL composition
E.g. a url address:
http://127.0.0.1:8088/docs/
format: Protocol IP address or domain name + path + requested
domain name is resolved to an IP address of the DNS server
- A url address is an interface, the interface is among the largest general use HTTP and HTTPS protocols, a hypertext transfer protocol;
- The domain is behind url path
- HTTP and HTTPS difference:
- HTTPS is an encrypted protocol, after some encryption algorithm, compared to HTTP safer;
- The default HTTP port 80
- HTTPS default port number 443
2. Request Structure
- Request the first line (request line)
- Request header (message header)
- Skip a line
- Request body (request body)
3. Request the first line
Request first line comprising: request method (GET) request address + + protocol version (HTTP / 1.1)
- Request method
- get
- Get the server resources (to retrieve data)
- The corresponding sql statement select
- No request body
- Pass parameters: Parameter request (query string query strings) placed url to? key1 = value form of .........
- Do not use when dealing with sensitive data, such as: account password
- post
- Often create (additional) resources
- Corresponds to insert into sql statement
- There is a request body
- Request parameter
- In the request body parameters (the request body)
- get
-
-
-
- text / plain text transmission is HTTP packet body is in plain text format and without any modifications, the server will take its own text processing;
- Form this is the most common way of transmission parameters, HTML tag corresponding thereto in both form, which itself uses the Key-Value parameter passing mode;
- json format is more effective than the place can be transmitted Form Object;
- File transfer a single file
- Depending on the Content-type, server to read the HTTP Body of parameters are not the same
-
- put
- Modify the server's resources
- Sql statement corresponds to the total update
- There is a request body parameters have
- delete
- Delete server resources
- Sql statement corresponds to the total delete from
- There is a request body parameters have
-
- Request address
- Protocol version (HTTP / 1.1)
4. The request header (message header)
User-Agent (the responsibility of children. Hey Gent) :( client type)
-
- Browser, mobile client
cookie:
-
- The equivalent of identity information, generally on the request header
- It does not save the user's non-sensitive information
The request body (request body)
- The vast majority did not get request requesting body
- The main role of carrying the request parameters
Second, the response packet
Response message is sent by the server
1. Request Structure
- In response to the first line (the status line)
- Response header (the message header)
- Skip a line
- Body response (response entity)
2, in response to the first line (status line)
include:
- Protocol version number
- Status of the response it
The most common status codes and their meanings:
- Successful- 2xx: successful class action is successful acceptance, understanding and adoption
- 200 OK
- Server successfully returned the data requested by the user
- Often in order to simplify the process
- After you create a successful POST should return 201
- 404 NOT FOUND
- Resource not found or path path does not exist
- 500 INTERNAL SERVER ERROR
- Internal server error
- The most common causes are: internal server hung up
- For example, you pass parameters Some parameters are empty, which led background code can not be resolved, abnormal collapse
Once common response codes and their meanings:
- restful api-style interfaces
- Successful - 2xx: successful class action is successful acceptance, understanding and adoption
- 201 CREATED
- Create a resource successfully passed POST or PUT
- 204 NO CONTENT
- Resource successfully modified, but did not return content
- DELETE operations commonly used in the return
- Redirect action type, in order to complete the request need to further perform: 3xx - Redirection.
- 301 permanent redirect
- 302 temporary redirect
- Client Error - 4xx: 客户端错误类,请求包含语法错误或者请求无法实现
- 401 UNAUTHORIZED
- 没有权限访问该资源
- 典型情况:用户没有登录,没有获得对应的access token而直接访问某资
- 403 FORBIDDEN
- 禁止访问
- 典型情况:虽然用户已登录,但是去更新/删除需要更高权限才能操作的资源
- 405 METHOD NOT ALLOWED
- 方法不允许
- 举例:比如某个资源不允许POST请求,但是你确发起了POST请求
3、响应头(消息报头)
- Content-Type(康泰的太婆)
- 代表:响应报文返回的数据类型/json格式、HTML格式页面报文
- set-Cookie
- 服务器将用户信息(session-id)放到Set-Cookie,浏览器拿到 set—cookie 然后将其放到浏览器的 Cookie 中保存
4、响应体(响应实体)
- 响应数据
- html页面或者json格式数据
- 前后端分离 开发模式
- 是以json格式返回数据
- 前后端不分离 开发模式
- 返回HTML的页面,需要后端和前端人员配合
- 204 没有响应体,主要看需求文档和开发心情
*******请大家尊重原创,如要转载,请注明出处:转载自:https://www.cnblogs.com/shouhu/ 谢谢!!*******