How to use curl


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

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


-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'

The order above willUser-AgentChange to Chrome browser.

$ curl -A ''

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'


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

$ curl -b 'foo=bar'

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

$ curl -b 'foo1=bar;foo2=bar2'

The above command sends two cookies.

$ curl -b cookies.txt

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


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

$ curl -c cookies.txt

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


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

$ curl -d'login=emma&password=123'-X POST
$ curl -d 'login=emma' -d 'password=123' -X POST

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'

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


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

$ curl --data-urlencode 'comment=hello world'

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


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

curl -e ''

The order above willRefererSet header to

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

curl -H 'Referer:'


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

$ curl -F '[email protected]'

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'

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'

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


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

$ curl -G -d 'q=kitties' -d 'count=20'

The above command will issue a get request. The actual URL of the request is。 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'


-HParameter to add the header of the HTTP request.

$ curl -H 'Accept-Language: en-US'

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

$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy'

The above command adds two HTTP headers.

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

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


-iParameter prints the HTTP header of the server response.

$ curl -i

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.


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

$ curl -I

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

--headParameters are equivalent to-I

$ curl --head


-kParameter specifies to skip SSL detection.

$ curl -k

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


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

$ curl -L -d 'tweet=hi'


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

$ curl --limit-rate 200k

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


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

$ curl -o example.html

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


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

$ curl -O

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


-sParameters will not output error and progress information.

$ curl -s

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


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

$ curl -s -o /dev/null

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


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

$ curl -u 'bob:12345'

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'

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


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

$ curl -v

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

$ curl --trace -


-xParameter specifies the proxy for the HTTP request.

$ curl -x socks5://james:[email protected]:8080

The above command specifies that the HTTP request passes the Socks5 proxy.

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

$ curl -x james:[email protected]:8080

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


-XParameter specifies the method of the HTTP request.

$ curl -X POST

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 […]