How to use curl

Time:2020-7-22

How to use curl

brief introduction

Curl is a common command-line tool used to request web servers. Its name is the meaning of the client’s URL tool.

It is very powerful, with dozens of command line parameters. If skilled, it can completely replace the graphical interface tools like postman.

How to use curl

This paper introduces its main command line parameters, as a daily reference, easy to consult. The content is mainly translated from curl cookbook. In order to save space, the following example does not include the output of the runtime. Beginners can read the curl beginner’s tutorial I wrote before.

Curl makes a get request without any parameters.


$ curl https://www.example.com

The order is towww.example.comWhen a get request is issued, the content returned by the server is output on the command line.

-A

-AParameter specifies the user agent header for the client, which isUser-Agent。 The default user agent string for curl iscurl/[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 order above willUser-AgentChange to Chrome browser.


$ curl -A '' https://google.com

The above command will be removedUser-AgentHeader.

It can also be done through-HParameters directly specify the header, changeUser-Agent


$ curl -H 'User-Agent: php/1.0' https://google.com

-b

-bParameter is used to send a cookie to the server.


$ curl -b 'foo=bar' https://google.com

The above command generates a headerCookie: foo=barTo the server by sending afoo, the value isbarCookie for.


$ curl -b 'foo1=bar;foo2=bar2' https://google.com

The above command sends two cookies.


$ curl -b cookies.txt https://www.google.com

The above command reads the local filecookies.txt, which contains the cookie set by the server (see-cParameter), and send it to the server.

-c

-cParameter to write the cookie set by the server to a file.


$ curl -c cookies.txt https://www.google.com

The above command writes the cookie set by the server’s HTTP response to a text filecookies.txt

-d

-dParameter is used to send the data body of the post request.

$ curl -d'login=emma&password=123'-X POST https://google.com/login
#Or
$ curl -d 'login=emma' -d 'password=123' -X POST  https://google.com/login

use-dAfter the parameter, the HTTP request is automatically headersContent-Type : application/x-www-form-urlencoded。 The request is automatically converted to a post method, so it can be omitted-X POST

-dParameter can read the data of local text file and send it to the server.


$ curl -d '@data.txt' https://google.com/login

Above command readdata.txtThe content of the file is sent to the server as a data body.

–data-urlencode

--data-urlencodeParameters are equivalent to-dThe difference is that the sent data will be URL encoded automatically.


$ curl --data-urlencode 'comment=hello world' https://google.com/login

In the code above, the data senthello worldThere is a space in between, which needs to be URL encoded.

-e

-eParameter is used to set the HTTP headerRefererRepresents the source of the request.


curl -e 'https://google.com?q=example' https://www.example.com

The order above willRefererSet header tohttps://google.com?q=example

-HParameters can be added directly by adding a headerRefererTo achieve the same effect.


curl -H 'Referer: https://google.com?q=example' https://www.example.com

-F

-FParameter is used to upload binary files to the server.


$ curl -F '[email protected]' https://google.com/profile

The above command adds a header to the HTTP requestContent-Type: multipart/form-data, and then put the filephoto.pngAsfileField upload.

-FParameter to specify the MIME type.


$ curl -F '[email protected];type=image/png' https://google.com/profile

The above command specifies that the MIME type isimage/pngOtherwise curl sets the MIME type toapplication/octet-stream

-FParameter can also specify a file name.


$ curl -F '[email protected];filename=me.png' https://google.com/profile

In the above command, the original file name isphoto.png, but the file name received by the server isme.png

-G

-GParameter is used to construct the query string for the URL.


$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

The above command will issue a get request. The actual URL of the request ishttps://google.com/search?q=kitties&count=20。 If omitted--GA post request is issued.

If the data needs URL encoding, it can be combined--data--urlencodeParameter.


$ curl -G --data-urlencode 'comment=hello world' https://www.example.com

-H

-HParameter to add the header of the HTTP request.


$ curl -H 'Accept-Language: en-US' https://google.com

The above command adds the HTTP headerAccept-Language: en-US


$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com

The above command adds two HTTP headers.


$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

The header of the HTTP request added by the above command isContent-Type: application/json, and then use-dParameter to send JSON data.

-i

-iParameter prints the HTTP header of the server response.


$ curl -i https://www.example.com

After receiving the server’s response, the above command first outputs the header of the server’s response, and then a blank line, and then outputs the source code of the web page.

-I

-IParameter sends a head request to the server and prints the HTTP header returned by the server.


$ curl -I https://www.example.com

The above command outputs the server’s response to the head request.

--headParameters are equivalent to-I


$ curl --head https://www.example.com

-k

-kParameter specifies to skip SSL detection.


$ curl -k https://www.example.com

The above command does not check whether the SSL certificate of the server is correct.

-L

-LParameter causes the HTTP request to follow the server’s redirection. Curl does not follow redirection by default.


$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet

–limit-rate

--limit-rateIt is used to limit the bandwidth of HTTP requests and responses and simulate a slow network speed environment.


$ curl --limit-rate 200k https://google.com

The above command limits the bandwidth to 200K bytes per second.

-o

-oParameter to save the server’s response to a file, equivalent towgetCommand.


$ curl -o example.html https://www.example.com

The order above willwww.example.comDeposit into stockexample.html

-O

-OParameter saves the server response as a file and takes the last part of the URL as the file name.


$ curl -O https://www.example.com/foo/bar.html

The above command saves the server response as a file namedbar.html

-s

-sParameters will not output error and progress information.


$ curl -s https://www.example.com

Once an error occurs in the above command, no error message will be displayed. If there is no error, the result will be displayed normally.

If you want curl to produce no output, you can use the following command.


$ curl -s -o /dev/null https://google.com

-S

-SParameter specifies that only error messages are output, usually with the-sTogether.


$ curl -s -o /dev/null https://google.com

There is no output from the above command unless an error occurs.

-u

-uParameter is used to set the user name and password for server authentication.


$ curl -u 'bob:12345' https://google.com/login

The above command sets the user name asbob, password is12345And then turn it into an HTTP headerAuthorization: Basic Ym9iOjEyMzQ1

Curl can identify the user name and password in the URL.


$ curl https://bob:[email protected]/login

The above command can identify the user name and password in the URL and turn it into the HTTP header in the previous example.


$ curl -u 'bob' https://google.com/login

The above command only sets the user name. After execution, curl will prompt the user to enter the password.

-v

-vThe whole process of parameter output communication is used for debugging.


$ curl -v https://www.example.com

--traceParameters can also be used for debugging, as well as outputting raw binary data.


$ curl --trace - https://www.example.com

-x

-xParameter specifies the proxy for the HTTP request.


$ curl -x socks5://james:[email protected]:8080 https://www.example.com

The above command specifies that the HTTP request passes throughmyproxy.com:8080From the Socks5 proxy.

If no proxy protocol is specified, the default is http.


$ curl -x james:[email protected]:8080 https://www.example.com

In the above command, the proxy for the request uses the HTTP protocol.

-X

-XParameter specifies the method of the HTTP request.


$ curl -X POST https://www.example.com

The order above is righthttps://www.example.comIssue a post request.

Recommended Today

DK7 switch’s support for string

Before JDK7, switch can only support byte, short, char, int or their corresponding encapsulation classes and enum types. After JDK7, switch supports string type. In the switch statement, the value of the expression cannot be null, otherwise NullPointerException will be thrown at runtime. Null cannot be used in the case clause, otherwise compilation errors will […]