Today I’ll take a look at HTTP requests and responses
The HTTP request
When we visit a web page, what data do we send to the server? The following takes visiting baidu home page as an example. We enter www.baidu.com in the browser, and the requested content includes:
GET / HTTP/1.1
Host: www.baidu.com
User-agent: curl/7.54.0
(the user-agent: Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36)
Accept: * / *(here means I Accept whatever you send back)
An HTTP request contains a maximum of four parts and a minimum of three parts.
-
Part I:
Method Path protocol/version
-
There are 8 methods in total, such as GET, POST, PUT, DELETE,PATCH, HEAD, OPTIONS, TRACE and CONNECT
-
The path contains query parameters, but does not include anchor points, because query parameters are for the server and anchor points are for the browser. The path must start with a ‘/’. If no path is written, the default is ‘/’.
-
Protocol/version such as HTTP /1.1
-
-
The second part
The format is key: value
Such as:
Host: www.baidu.com user-agent: curl/7.54.0 Accept: */* Content-length: 10 Application (/x-www-form-urlencoded) (Application: application data WWW: Web from: forms urlencoded: Data is compressed with urlencoded)Copy the code
You can put any information that you want to tell the server, in this key:value format. The Content-Type of Part 2 identifies the format of Part 4
-
The third part
!!!!! Note: The third part is a carriage return, which is required so that the server knows where part 4 and Part 2 are cut off
-
The fourth part
The fourth part is the data to be uploaded, such as user name and password, avatar, nickname and so on. The fourth section can also be empty.
Use Chrome Developer Tools to view HTTP requests
- Open your browser and go to the Baidu home page
- Right click check, then click Network
- Enter HTTPS //www.baidu.com in the address box and press Enter
- Click on the first Request and find the Request Headers in the box on the right. Click on the small arrow to expand the data, and then click on the View Source on the right.
- Click the Preserve Log on your browser so you can see the request instead of seeing it because the page has refreshed
The HTTP response
After the browser makes a request to the server, the server will give a response, so what is the format of the response?
The response is divided into four parts
-
The first part
Protocol/Version Status code Status description
Status code:
4xx - Browser request error, such as wrong semantics, wrong parameters, etc. 5XX - server errorCopy the code
-
The second part
The format is key: value
Content-Length: 198 Content-Type: text/html ...... Copy the code
Content-type annotates the format of Part 4, following the MIME specification
-
The third part
enter
-
The fourth part
Content to download, this section can be a long, long, long (note!! The object is not a json object, but a string.
View HTTP responses with Chrome Developer Tools
- Open your browser and go to the Baidu home page
- Right click check, then click Network
- Enter HTTPS //www.baidu.com in the address box and press Enter
- Click on the first request and find the Response Headers in the box on the right. Click on the small arrow to expand the data, and then click on the View Source on the right. The fourth part is in Response.
How to use the curl command
Curl is an open source file transfer tool that works on the command line using URL syntax. Using the curl command, you can send a request to the server.
Curl – s-v — “https://www.baidu.com/s?wd=java”
Request information
GET /s? Wd = Java HTTP/1.1 Host: www.baidu.com user-agent: curl/7.43.0 Accept: */*Copy the code
Response information
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 227
Copy the code
. I’m not going to copy many more
Request methods can also be added
curl -X POST -d "1234567890" -s -v -H "Frank: xxx" -- "https://www.baidu.com"
Copy the code
GET & POST
Get: Gets the content
Post: Uploads content
Get can also have a lot of data in the request, but generally the server will not receive it, the server will think that you come to me to get things and send things to me, which is not so good, generally not like this. Take things as you take them, don’t take them and upload them