Http basics
The client sends a request (request message) to the server, and the request content includes request line, request header, and request body.
The server responds to the data returned by the client, and the http response (response message) includes status line, response header, and entity content.
The specific content of the request can be viewed in inspect-network. The first item under All-Name (the URL item) is sent to the request. There are General, Response Headers and Request Headers in the Headers after clicking, These are the information organized by the browser.
Request message:
Request line
method url
GET/produc_detail?id=2
POST/login
Multiple request headers
Host: www.google.com
Cookie[^1]: SEARCH_SAMESITE=CgQIl5QB; HSID=Acx6gym1ffa3ppTqI; SSID=ABqoRPct1dz_XB-1y; …
Content-Type: apllication/x-www-form-urlencoded or application/json
Content-type is the request body content type.
Request body
The request body does not necessarily have. A GET request has no request body, a POST request [^2] has no request body without parameters, and a POST request has a request body with parameters.
username=tom&pwd=123 This is Content-Type: apllication/x-www-form-urlencoded format.
{“username”: “tom”, “pwd”: “123”} This is Content-Type: application/json format.
Response message:
Response status line
status Status code, such as 200, 201, 401, 404, 500. (one-to-one correspondence with the status text example)
statusText Status text such as OK, Created, Unauthorized, Not Found, Internal Server Error
Multiple response headers
Content-Type: text/html; (text in html format, text/json in JSON format) charest=utf-8 (encoding)
Set-Cookie: BD_CK_SAM=1; path=/ The server returns the cookie data, corresponding to the cookie in the request header.
Response body
html text/json text/js/css/image..
Different types of requests and their functions
The same server address sends different types of requests, and the server does different things.
GET: read data from the server
POST: add new data to the server
PATCH: Add new data to the server (partial update)
PUT: Update the existing data on the server side (overall update) (in practice, it is often mixed with POST)
DELETE: Delete server-side data
Ajax request
There are two ways to send Ajax requests: XHR objects, and the
catch()
function.
Classification of API
The API here refers to the interface for front-end and back-end interaction.
REST API: restful (another name)
Send a request for CRUD (CRUD: the English abbreviation of CRUD, create/read/update/delete) which operation is determined by the request method
is actually a different type of request in section Axios.1.2. If the server supports different request types corresponding to different operations, it is restful; otherwise, it is restless.
The same request path can perform multiple operations
The request method will use GET/POST/PUT/DELETE
Non-REST API: restless (alias)
The request method does not determine the requested CRUD operation
A request path corresponds to only one operation
Generally only GET/POST (GET for query, POST for addition, deletion and modification)
Appendix
[^1]: If it is unsuccessful, it is a problem with the installation of json-server
There are 3 ways to solve the installation problem. The first is to add npx before the code, the second is to open a cmd in the current directory and type the code, and the third is to modify permissions through PowerShell in administrator mode [About execution policy](https://docs.microsoft. com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2).
Admin Mode PowerShell:
Press Win + R.
Type powershell and press Ctrl+Shift+Enter.
Click OK to start PowerShell with administrator privileges.
PowerShell By default, *.ps1 script files are not allowed to be executed. Running the ps1 file gives the following error:
File C:\Temp\Test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
The following command can resolve the above error
PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned
[^2]: db.json code
1 | { |