This is the 15th day of my participation in the August Challenge. Check out the August Challenge for more details

preface

Curl curl curl curl curl curl curl curl curl curl curl Now, let’s start our introductory journey.

Use of the curl command

A GET request

curl -s -v -H "xxx: yyy" -- "https://www.baidu.com"
Copy the code

Here is a brief description of some parameters:

  • -sDo not display a progress bar
  • -vRequest and response are displayed
  • -HAdd a response header
  • www.baidu.com requested website
  • "xxx: yyy"I don’t have to write it. Same thing

I don’t know why in Windows, I put the content to download first, and there are a lot of other things, so here is the important content

In the picture above*Representative isannotation.>Represents the content of the request,<Represents the content of the response

Content of request:

Requested content meaning
GET / HTTP/1.1 Get the root directory, using the protocol HTTP1.1
Host: www.baidu.com Requested url
The user-agent: curl / 7.70.0 Use curl/7.70.0 to initiate the request
Accept:  / Accept whatever you return
xxx: yyy The command above has this, and the content of the request below will also show this
enter

The content of the response:

Content of the response meaning
HTTP / 1.1 200 OK The protocol is HTTP. The version number is 1.1. The status code is 200
Content-Length: 2443 The length of the response content
Content-Type: text/html Content-type Specifies the format of the annotation response
enter

A POST request

curl -X POST -s -v -H "xxx: yyy" -- "https://www.baidu.com"
Copy the code

Content of request:

> POST/HTTP/1.1 > Host: www.baidu.com > user-agent: curl/7.70.0 > Accept: */* > XXX: yyy >Copy the code

The content of the response:

< HTTP/1.1 302 Found < Connection: keep-alive < Content-Length: 17931 < Content-type: text/ HTML < Date: Sun, 22 Aug 2021 13:51:31 GMT < Etag: "54D9748E-460b" < Server: BFE /1.0.8.18 <Copy the code

The POST request carries data

curl -X POST -d "1234567890" -s -v -H "xxx: yyy" -- "https://www.baidu.com"
Copy the code

Content of request:

> POST/HTTP/1.1 > Host: www.baidu.com > user-agent: curl/7.70.0 > Accept: */* > XXX: yyy > content-length: 10 > Content-Type: application/x-www-form-urlencoded >Copy the code

The content of the response:

< HTTP/1.1 302 Found < Connection: keep-alive < Content-Length: 17931 < Content-type: text/ HTML < Date: Sun, 22 Aug 2021 13:54:05 GMT < Etag: "54D9748E-460b" < Server: BFE /1.0.8.18 <Copy the code

The HTTP request

The format of the HTTP request (included parts)

Part of the content
Part 1 Verb path protocol/version
Part 2 Key1: value1
Part 2 Key2: value2
Part 2 Content-Type: application/x-www-form-urlencoded
Part 2 Host: www.baidu.com
Part 2 The user-agent: curl / 7.70.0
Part 3
Part 4 Data to upload

PS.

  • The request contains a maximum of 4 parts and a minimum of 3 parts (part 4 can be empty)
  • Part 3 is always a carriage return (\n) that separates part 2 from part 4
  • GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
  • This path contains “query parameters”, but does not include “anchors”, unlike the URL
  • If you did not write the path, the default path is /, if you requested the url www.baidu.com/s?wd=hello, the path is /s? Wd =hello
  • The Content-Type in Part 2 annotates the format of Part 4

Use Chrome Developer Tools to view HTTP requests

Open your browser, right click Check/press F12 to open Chrome Developer Tools

Enter the url in the browser’s address bar

Click on the Network to view Request Headers and click on “View Source” to see the first three parts of the Request

If (POST) only has the fourth part of the request, it can be seen in FormData or Payload.

The HTTP response

The format of the HTTP response (included parts)

Part of the content
Part 1 Protocol/Version Status code Status description
Part 2 Key1: value1
Part 2 Key2: value2
Part 2 Content-Length: 2443
Part 2 Content-Type: text/html
Part 3
Part 4 Content to download

PS.

  • The status code is memorized, which is what the server says to the browser
    • 1 xx is not commonly used
    • 2xx: Success (200 Common success,204 Created success (POST))
    • 3xx means get out of here (301 permanently moved, part 2 tells new address; [Fixed] [fixed] [fixed] [fixed] [fixed] [fixed] [fixed] [fixed] [fixed] [fixed] [fixed] 304 The content of this time is the same as the content of the last time, it is a type of roll)
    • 4xx means you are wrong
    • 5xx means ok, I was wrong
  • State explanations don’t help, right
  • The responses to GET and POST requests can be the same or different

Use Chrome Developer Tools to view HTTP requests

The process is similar to the request, but I won’t go into details here; the differences have been highlighted in bold

  1. Open your browser, right click Check/press F12 to open Chrome Developer Tools
  2. Enter the url in the browser’s address bar
  3. Click on Network to view Response Headers and click on “View Source” to see the first three parts of the request
  4. Look at Response or Preview, and you’ll see part 4 of the Response

If there are any mistakes in this article, please correct them in the comments section. If this article helped you or you liked it, please like it and follow it.