Curl usage guide-2

This article introduces its main command line parameters as a daily reference for easy reference. The content is mainly translated from "curl cookbook" . In order to save space, the following example does not include the runtime output. For beginners, you can read the "curl tutorial for beginners" I wrote before .

Without any parameters, curl will issue a GET request.


$ curl https://www.example.com

The above command sends www.example.coma GET request to the server, and the content returned by the server will be output on the command line.

-A

-AThe parameter specifies the user agent header of the client, ie User-Agent. The default user agent string for curl 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 command will be User-Agentchanged to Chrome browser.


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

The above command will remove the User-Agentheader.

You can also -Hdirectly specify the header through parameters and change it User-Agent.


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

-b

-bParameters are used to send cookies to the server.


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

The above command will generate a header Cookie: foo=barand send a cookie with a name fooand a value to the server bar.


$ 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 file cookies.txt, which contains the cookie set by the server (see -cparameters), and sends it to the server.

-c

-cThe parameter writes the cookie set by the server to a file.


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

The above command writes the cookies set by the server's HTTP response to a text file cookies.txt.

-d

-dThe parameter is used to send the data body of the POST request.


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

After using -dparameters, HTTP requests will automatically add headers Content-Type : application/x-www-form-urlencoded. And it will automatically convert the request to the POST method, so it can be omitted -X POST.

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


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

The above command reads data.txtthe content of the file and sends it to the server as a data body.

--data-urlencode

--data-urlencodeThe parameters are equivalent to -dthe data body of the POST request. The difference is that the sent data will be automatically URL-encoded.


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

In the above code, hello worldthere is a space between the sent data , which needs to be URL encoded.

-e

-eThe parameter is used to set the HTTP header Referer, indicating the source of the request.


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

The above command sets the Refererheader to https://google.com?q=example.

-HParameters can be directly added to the header Refererto achieve the same effect.


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

-F

-FParameters are used to upload binary files to the server.


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

The above command will add a header to the HTTP request Content-Type: multipart/form-data, and then upload the file photo.pngas a filefield.

-FThe parameter can specify the MIME type.


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

The above command specifies the MIME type image/png, otherwise curl will set the MIME type application/octet-stream.

-FThe parameter can also specify the file name.


$ 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 received by the server me.png.

-G

-GThe parameters are used to construct the query string of the URL.


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

The above command will issue a GET request, and the actual requested URL is https://google.com/search?q=kitties&count=20. If omitted --G, a POST request will be issued.

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


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

-H

-HThe parameter adds the header of the HTTP request.


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

The above command adds HTTP headers Accept-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 above command adds the header of the HTTP request Content-Type: application/json, and then -dsends the JSON data with parameters.

-i

-iThe parameter prints out the HTTP header of the server response.


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

After the above command receives the server's response, it first outputs the header of the server's response, then a blank line, and then outputs the source code of the webpage.

-I

-IThe parameter sends a HEAD request to the server, and then the HTTP header returned by the server is printed out.


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

The above command outputs the server's response to the HEAD request.

--headThe parameters are equivalent to -I.


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

-k

-kThe parameter specifies to skip SSL detection.


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

The above command will not check whether the server's SSL certificate is correct.

-L

-LThe parameter causes the HTTP request to follow the redirection of the server. curl does not follow redirects by default.


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

--limit-rate

--limit-rateUsed to limit the bandwidth of HTTP requests and responses to simulate a slow Internet speed environment.


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

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

-O

-oThe parameter saves the server's response as a file, which is equivalent to a wgetcommand.


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

The above command will be www.example.comsaved as example.html.

-O

-OThe parameter saves the server response as a file, and uses 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 with the file name bar.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 no error occurs, the operation result will be displayed normally.

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


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

-S

-SThe parameter specifies that only error information is output, and is usually -sused with it.


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

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

-u

-uThe parameters are 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 boband password to 12345, and then converts it to an HTTP header Authorization: Basic Ym9iOjEyMzQ1.

curl can recognize the username and password in the URL.


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

The above command can recognize the username and password in the URL and convert them to 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, and raw binary data will be output.


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

-x

-xThe parameter specifies the proxy for the HTTP request.


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

The above command specifies myproxy.com:8080the socks5 proxy through which the HTTP request is sent.

If the proxy protocol is not specified, the default is HTTP.


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

In the above command, the requested proxy uses the HTTP protocol.

-X

-XThe parameter specifies the method of the HTTP request.


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

The above command pair https://www.example.comissues a POST request.

Guess you like

Origin blog.csdn.net/weixin_43272542/article/details/115291073