**HTTP It's a kind of being able to get things like HTML This kind of network resources **protocol( Communication protocol ). It's in Web On the basis of data exchange , It's a kind of client-server agreement , in other words , Requests are usually initiated by recipients like browsers . A complete Web Documents are usually composed of different subdocuments , It's like text 、 Layout description 、 picture 、 video 、 Script, etc. .
1. HTTP The basic process of request interaction
- Before and after application sends from browser to server HTTP request ( Request message )
- After the background server receives the request , The scheduling server application processes the request , Back to browser HTTP Respond to ( response message )
- The browser side receives a response , Parse display response body / Call the monitor callback
2. HTTP Request message
- Request line :method url: GET/url, POST/url
- Multiple request headers
Host: www.baidu.com
Cookie: BAIDUID=AD3B0FA706E; BIDUPSID=AD3B0FA706;
Content-Type: application/x-www-form-urlencoded perhaps application/json
- Request body
username=tom&pwd=123
{“username”: “tom”, “pwd”: 123}
3. HTTP response message
- Response status line : status statusText
- Multiple response headers
Content-Type: text/html;charset=utf-8
Set-Cookie: BD_CK_SAM=1;path=/ - Response body
html Text /json Text /js/css/ picture …
4. post Request body parameter format
- Content-Type:application/x-www.form-urlencoded;charset=utf-8
Used for key value pair parameters , The key value of the parameter is used = Connect , Use... Between parameters & Connect
for example : name=%E5%B0%8F%E6%98%8E&age=12
- Content-Type:application/json;charset=utf-8
be used for json String parameters
for example : {“name”: “%E5%B0%8F%E6%98%8E”, “age”: 12}
- Content-Type: multipart/form-data
For file upload requests
5. Common response status codes
200 OK The request is successful . Commonly used in GET And POST request
201 Created Created . Successfully requested and created a new resource
401 Unauthhorized unauthorized / Request for user authentication
404 Not Found The server could not find the resource at the request of the client
500 Internal Server Error Server internal error , Unable to complete request
6. Different types of requests and functions
- GET: Read data from the server side
- POST: Add new data to the server side
- PUT: Update the data on the server side
- DELETE: Delete server side data
7. API The classification of
-
REST API: restful
(1) Send a request for CRUD Which operation is determined by the way the request is made(2) Multiple operations can be performed on the same request path
(3) The request method will use GET/POST/PUT/DELETE -
Not REST API: restless
(1) The way you ask doesn't determine what you ask for CRUD operation(2) A request path corresponds to only one operation
(3) Usually only GET/POST
9. Use **json-server ** build REST API
9.1 json-server What is it? **?**
To build quickly REST API The toolkit
9.2 How to use json-server
- Global installation json-server
npm install -g json-server
- Create the database in the target root directory json file :db.json
{
"posts": [
{
"id": 1, "title": "json-server", "author": "typicode" }
], "comments": [
{
"id": 1, "body": "some comment", "postId": 1 } ],
"profile": {
"name": "typicode" } }
- Start the server and execute the command
json-server --watch db.json
summary :
This article mainly shared about http Relevant technical knowledge , For later study axios Lay the foundation .