background
Online shopping m.fenfaw.com cn- Seeing that developers use this command to adjust the interface, I also use it
- It's really convenient. Sometimes when you want to tell the developer that there may be a problem with an interface, you can't take screenshots every time. In this way, the credibility is not high
- Therefore, writing curl to call the interface is the fastest. If you directly let the development copy and paste it again, you will know that there is a problem with the interface
curl introduction
- curl is a common command-line tool used to request Web servers
- Its name means the URL tool of the client
- Its function is very powerful, and there are dozens of command line parameters
- If you are proficient, you can completely replace the Postman interface testing tool
Official documents
https://curl.se/docs/manpage.html
Supported protocols
- DICT,FILE,FTP,FTPS,GOPHER,HTTP,HTTPS
- IMAP,IMAPS,LDAP,LDAP,POP3,POP3,RTMP,RTSP,SCP,SFTP,SMB
- SMBS,SMTP,SMTPS,TELNET,TFTP
Provide powerful functions
- Agent support
- User authentication
- FTP upload
- httppost
- SSL connection
- cookies
- File transfer recovery
- Metalink
- wait
Syntax format
curl [options / URLs]
Various transmission methods of URL
It's not necessary to use it in practical work if you only expand it
Braces {}
http://site.{1,2,3}.com
It is equivalent to sending a request for three host s
http://site.1.com http://site.2.com http://site.3.com
Brackets [] for alphanumeric sequence
Similar range (1100)
ftp://ftp.example.com/file[1-100].txt file # 100 requests ftp://ftp.example.com/file[001-100].txt (with 0) # 100 requests ftp://ftp.example.com/file[a-z].txt file # 26 requests
Note: nested sequences are not supported, but you can use multiple nested sequences next to each sequence
Comprehensive use
http://example.com/archive[1996-1999]/vol[1-4] / part {a, b, c} html
[] combined step
http://example.com/file[1-100:10].txt files # are fetched every 10, with a total of 10 requests http://example.com/file[a-z:2].txt files # are fetched once every 2, with a total of 13 requests
Common parameters
Without parameters
Request web address
https://www.cnblogs.com/poloyy/
The html source code of the web page is returned
If it is a normal get request
be careful
In the following chestnuts, you will add - v basically to see the detailed process of the request and make it easier to see that the corresponding parameters have taken effect. In actual use, you don't need to add - v every time
-A
- Equivalent parameters: - user agent < name >
- Role: specify the user agent of the client
curl -v -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36" http://baidu.com
-b
- Equivalent parameters: - Cookie < data|file >
- Function: send cookies to the server, which can be data or a file
curl -v -b 'foo=bar' http://baidu.com
Request Headers will generate a Cookie: foo=bar, and send a Cookie named Foo and value bar to the server
By printing the details, you can see that the request header is indeed added
-c
- Equivalent parameters: - Cookie Jar < filename >
- Function: write the Cookie returned by the server to be set into a file
curl -k -v -c test.txt https://www.baidu.com/s?wd=123%E8%89%BE%E5%BE%B7&rsv_spt=1&rsv_iqid=0xf0b9806f0000107b&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=tb&rsv_sug3=6&rsv_sug1=2&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=1138&rsv_sug4=1138
The Cookie that Baidu needs to set in response will be written to test Txt file
-d (common)
- Equivalent parameters: - Data < data >
- Function: used to send the data body of POST request
curl -v -d 'wd=biying' -d 'ie=UTF-8' https://www.baidu.com/s
- Http request header will automatically add # content type: application / x-www-form-urlencoded
- And the request will be automatically converted to the POST method, so - X POST can be omitted
--Data URLEncode (common)
Function: the parameter is equivalent to - d, which sends the data body of POST request, but it will automatically URL encode the sent data
curl -v -G --data-urlencode 'wd=b i y i n g' -d 'ie=UTF-8' https://www.baidu.com/s
The spaces are URL encoded
curl -v -G -d 'wd=b i y i n g' -d 'ie=UTF-8' https://www.baidu.com/s
-d will not url code
--Data raw (common)
Function: POST request body, which can receive a complete json string
curl --location --request POST 'http://test.com/account.login?ver=1.0&df=json&cver=3.7.8&os=android' \n--header 'Content-Type: application/json' \n--data-raw '{ "id":"123", "service":"account.login", "client":{ "ve":"3.7.8", "os":"android", "si":"123", "ex":{ "brand":"vivo", "dpfr":"8.1.0", }, "empty":false }, "data":{ "ex":{ "token":"123" } } }'
-e
- Price parameters: - referer < URL >
- Function: set the reference in HTTP Headers to indicate the source of the request
curl -v -e "test" http://baidu.com
-F
- Equivalent parameters: - form < name = content >
- Function: upload binary files to the server
curl -F 'file=@photo.png' https://google.com/profile
Automatically add "content type: multipart / form data" to HTTP Request Headers, and then add the file "photo" Png , upload as the value of , file , field
Specify MIME type
curl -F 'file=@photo.png;type=image/png' https://google.com/profile
Specify the MIME type as "application / octet stream", otherwise curl will set the MIME type as "application / octet stream"
specify a filename
curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
The file name received by the server is {me png
-G
- Equivalent parameters: - get
- Function: construct query string of URL
curl -v -G -d 'wd=biying' -d 'ie=UTF-8' https://www.baidu.com/s
Originally, - d will make the HTTP request become POST, but because - G is added, it is still GET because it is a query string
-H (common)
- Equivalent parameters: - header < header / @ File >
- Function: add HTTP request header
curl -v -H "token:123" -H "Content-type:application/json" http://baidu.com
-H specifies two request header fields, both of which are added
-i
- Equivalent parameters: - include
- Function: print response headers and response contents
curl -i http://baidu.com
-I
- Equivalent parameters: -- head
- Function: print only response headers
curl -I http://baidu.com
-k
- Equivalent parameters: - insecure
- Function: skip SSL detection
curl -k -I https://www.baidu.com
The HTTPS request can be initiated normally without visual inspection
-L
- Equivalent parameters: - location
- Function: make HTTP requests follow the redirection of the server. curl does not follow the redirection by default
curl -L -d 'tweet=hi' https://api.twitter.com/tweet
Suggestions are added
--limit-rate
Function: limit the bandwidth of HTTP request and response, and simulate the environment of slow network speed
curl -v --limit-rate 2k http://baidu.com
Limit bandwidth to 2K bytes per second
But I tested it and felt it was chicken ribs. I still completed the request in an instant
-o
- Equivalent parameters: - output < File >
- Function: save the server's Responses as a file, which is equivalent to the "wget" command
curl -o baidu.html http://baidu.com
-O
- Equivalent parameters: - remote name
- Function: save the server Responses as a file, and take the last part of the URL as the file name
-s
- Equivalent parameter: - silent
- Function: in silent mode, no error and progress information will be output. If no error occurs, the operation results will be displayed normally
-S
- Equivalent parameters: - show error
- Function: only output error information, which will make the - s parameter ineffective
Common combination technology
curl -S -s https://google.com/login
If it is correct, it will be output normally. If it is wrong, only error information will be output, and operation results will not be output
-u
- Equivalent parameters: - user < user: password >
- Function: set the user name and password of server authentication
curl -u 'bob:12345' https://google.com/login
-v
- Equivalent parameters: - verbose
- Function: output the whole process of communication for debugging
There are chestnuts all the way. Don't lift chestnuts any more
--trace
Function: the whole process of output communication is more detailed than - v
Not to mention chestnuts, because it is the result of hexadecimal data. Look directly at the following -- trace ASCII. The usage is the same, and the output data is the same, but it is displayed in hexadecimal
--trace-ascii
Function: the whole process of output communication is more detailed than - v, but there is no hexadecimal output, but decimal
The ultimate practical chestnut
In order to see the whole communication link of the request more comprehensively, I directly used the last interface of my work, but I changed all the sensitive information, so the request is impossible
curl --trace-ascii - --location --request POST 'http://test.com/account.login?ver=1.0&df=json&cver=3.7.8&os=android' \n--header 'Content-Type: application/json' \n--data-raw '{ "id":"123", "service":"account.login", "client":{ "ve":"3.7.8", "os":"android", "fr":"API Level-27", "gameId":"100000", "channelId":"100000", "si":"123", "ex":{ "brand":"vivo", "dpfr":"8.1.0", "imei":"123", "imsi":"123", "mac":"123", "mf":"vivo", "mobile":"", "model":"123", "net":"wifi", "orient":"P", "packageName":"123", "resX":"1080", "resY":"2034", "subChannelId":"", "utdid":"123", "versionCode":"1", "versionName":"3.7.8" }, "empty":false }, "data":{ "ex":{ "token":"123" } } }'
Request part
Response part
It can be said that it is very detailed. Even the byte size of each field is returned
-w
- Equivalent parameter: - > write format
- Function: specify the output format after completing the request
- Separate article explanation, more multivariable
https://www.cnblogs.com/poloyy/p/14877100.html
-X (common)
- Equivalent parameters: - Request < command >
- Function: specify the Method of HTTP request
curl -v -X POST http://baidu.com curl -v --request POST http://baidu.com
Common templates in work
It may not be completely universal and can be modified according to your own needs
GET request
curl -L -S -s -K http://baidu.com
POST request
curl -L -S -s -K -X post \n-H "Content-type:application/json" \n-H ".." \n--data-raw " { "a":123, "b:123 }"