Reading guide
curl Is a common command line tool , Used to request Web The server . Its name is client (client) Of URL Tool means .
It's very powerful , There are dozens of command line parameters . If you are skilled , It can completely replace Postman This kind of GUI tool .
This article introduces its main command line parameters , As a daily reference , Convenient access . The content is mainly translated from 《curl cookbook》. To save space , The following example does not include run-time output , Beginners can read what I wrote before 《curl Beginner tutorial 》.
Without any parameters ,curl Just send out GET request .
$ curl https://www.example.com
The above order is directed to www.example.com issue GET request , The content returned by the server will be output on the command line .
- -A Parameter specifies the client's user agent header , namely User-Agent.curl The default user agent string for is curl/[version].
$ curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com
The above order will User-Agent Change to Chrome browser .
$ curl -A '' https://google.com
The above command will remove User-Agent header .
It can also be done through -H Parameter specifies the header directly , change User-Agent.
$ curl -H 'User-Agent: php/1.0' https://google.com
- -b Parameter to send... To the server Cookie.
$ curl -b 'foo=bar' https://google.com
The above command generates a header Cookie: foo=bar, Send a message to the server called foo、 The value is bar Of Cookie.
$ curl -b 'foo1=bar' -b 'foo2=baz' https://google.com
The above command sends two Cookie.
$ curl -b cookies.txt https://www.google.com
The above command reads the local file cookies.txt, It's set by the server Cookie( See -c Parameters ), Send it to the server .
- -c Parameter sets the server to Cookie Write a file .
$ curl -c cookies.txt https://www.google.com
The above command will send the HTTP Respond to the set Cookie Write text file cookies.txt.
- -d Parameters are used to send POST Requested data body .
$ curl -d'login=emma&password=123'-X POST https://google.com/login
# perhaps
$ curl -d 'login=emma' -d 'password=123' -X POST https://google.com/login
Use -d After the parameters ,HTTP The request will be automatically header Content-Type : application/x-www-form-urlencoded. And will automatically turn the request to POST Method , Therefore, it can be omitted -X POST.
- -d Parameter can read the data of the local text file , Send to server .
$ curl -d '@data.txt' https://google.com/login
The command above reads data.txt The content of the document , Send... As data body to the server .
- --data-urlencode
--data-urlencode The parameter is equal to -d, send out POST Requested data body , The difference is that the data will be sent automatically URL code .
$ curl --data-urlencode 'comment=hello world' https://google.com/login
In the above code , Data sent hello world There is a space between , Need to carry out URL code .
- -e Parameter to set HTTP Header for Referer, Indicates the source of the request .
curl -e 'https://google.com?q=example' https://www.example.com
The above order will Referer Header set to https://google.com?q=example.
- -H Parameters can be added directly by header Referer, Achieve the same effect .
curl -H 'Referer: https://google.com?q=example' https://www.example.com
- -F Parameters are used to upload binaries to the server .
$ curl -F '[email protected]' https://google.com/profile
The above order will give HTTP Request with header Content-Type: multipart/form-data, Then put the file photo.png As file Field upload .
- -F Parameters can be specified MIME type .
$ curl -F '[email protected];type=image/png' https://google.com/profile
The above order specifies MIME The type is image/png, otherwise curl Will be able to MIME The type is set to application/octet-stream.
- -F Parameter can also specify the filename .
$ curl -F '[email protected];filename=me.png' https://google.com/profile
In the above command , The original file name is photo.png, But the file name that the server received was me.png.
- -G Parameters are used to construct URL The query string for .
$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
The above command will issue a GET request , Actually requested URL by https://google.com/search?q=k.... If omitted --G, It will send out a POST request .
If the data requires URL code , Can combine --data--urlencode Parameters .
$ curl -G --data-urlencode 'comment=hello world' https://www.example.com
- -H Parameter add HTTP Request header .
$ curl -H 'Accept-Language: en-US' https://google.com
The above command adds HTTP header Accept-Language: en-US.
$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
The above command adds two HTTP header .
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
The above command adds HTTP The request header is Content-Type: application/json, And then use -d Parameter sending JSON data .
- -i Parameters print out the response of the server HTTP header .
$ curl -i https://www.example.com
When the above command receives a response from the server , First output the header of the server response , Then leave a blank line , Then output the source code of the web page .
- -I Parameter to the server HEAD request , It will return the server HTTP The header is printed .
$ curl -I https://www.example.com
The above command outputs the server pair HEAD A response to a request .
- --head The parameter is equal to -I.
$ curl --head https://www.example.com
- -k Parameter specifies to skip SSL testing .
$ curl -k https://www.example.com
The above command will not check the server SSL Is the certificate correct .
- -L Parameters will make HTTP Request follow server redirection .curl Default does not follow redirection .
$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet
- --limit-rate Used to restrict HTTP Bandwidth of requests and responses , Simulate slow network speed environment .
$ curl --limit-rate 200k https://google.com
The above command limits the bandwidth to... Per second 200K byte .
- -o Parameter to save the response of the server as a file , Equate to wget command .
$ curl -o example.html https://www.example.com
The above order will www.example.com Save as example.html.
- -O Parameter to save the server response as a file , And will URL The last part of is the file name .
$ curl -O https://www.example.com/foo/bar.html
The above command saves the server response as a file , The file named bar.html.
- -s Parameters will not output error and progress information .
$ curl -s https://www.example.com
Once the above command is wrong , No error messages will be displayed . If there is no mistake , The operation result will be displayed normally .
If you want to let curl No output , You can use the following command .
$ curl -s -o /dev/null https://google.com
- -S Parameter specifies that only error messages are output , Usually with -o Use it together .
$ curl -s -o /dev/null https://google.com
The above command has no output , Unless something goes wrong .
- -u Parameter is used to set the user name and password of server authentication .
$ curl -u 'bob:12345' https://google.com/login
The above command sets the user name to bob, The password for 12345, Then turn it into HTTP header Authorization: Basic Ym9iOjEyMzQ1.
curl Able to identify URL The user name and password inside .
$ curl https://bob:[email protected]/login
The above command can recognize URL The user name and password inside , Turn it into the HTTP header .
$ curl -u 'bob' https://google.com/login
The above command only sets the user name , After execution ,curl The user will be prompted to enter the password .
- -v The whole process of parameter output communication , For debugging .
$ curl -v https://www.example.com
- --trace Parameters can also be used for debugging , It also outputs raw binary data .
$ curl --trace - https://www.example.com
- -x Parameter assignment HTTP The requested agent .
$ curl -x socks5://james:[email protected]:8080 https://www.example.com
The above order specifies HTTP Request by myproxy.com:8080 Of socks5 Sent by agent .
If no agency agreement is specified , The default is HTTP.
$ curl -x james:[email protected]:8080 https://www.example.com
In the above command , The requested agent uses HTTP agreement .
- -X Parameter assignment HTTP Requested method .
$ curl -X POST https://www.example.com
The order is right https://www.example.com issue POST request .
_ author : Ruan Yifeng
original text : http://www.ruanyifeng.com/blo...