curl usage guidelines
Tencent college classroom thanks NEXT sponsored site, Tencent official front-end courses free trial to learn.
HTTP: // www.ruanyifeng.com/blog/2019/09/curl-reference.html another article that she has to find time to carefully test.
Brief introduction
curl is commonly used command-line tool, used to request the Web server. Its name is the meaning of the client (client) of the URL tool.
It's very powerful, as many as dozens of command-line parameters. If skilled then Postman can replace this type of graphical interface.
This article describes its main command-line parameters, as a daily reference for easy access. Mainly translated from "curl Cookbook" . To save space, the following examples do not include output runtime, beginners can look at my previous write "curl tutorial for beginners" .
When no parameters, curl is to issue a GET request.
$ curl https://www.example.com
The above command to www.example.com
issue a GET request, the content server will be returned in the command line output.
-A
-A
Parameter specifies the client user agent header, i.e. User-Agent
. curl default user agent string 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 User-Agent
change the Chrome browser.
$ curl -A '' https://google.com
The above command will remove User-Agent
header.
You can also -H
change the parameters directly specify header User-Agent
.
$ curl -H 'User-Agent: php/1.0' https://google.com
-b
-b
Cookie parameters used to send to the server.
$ curl -b 'foo=bar' https://google.com
The above command generates a header Cookie: foo=bar
, transmitting to the server a name foo
, a value bar
of Cookie.
$ curl -b 'foo1=bar' -b 'foo2=baz' https://google.com
The above command transmits two Cookie.
$ curl -b cookies.txt https://www.google.com
The above command reads local files cookies.txt
, which are cookies (see server set -c
parameters), and sends it to the server.
-c
-c
Cookie parameter settings to a file server.
$ curl -c cookies.txt https://www.google.com
The above command will respond to HTTP server set Cookie written text file cookies.txt
.
-d
-d
Parameters used to transmit data POST request body.
$ 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
Used -d
after parameters, HTTP request header is automatically added Content-Type : application/x-www-form-urlencoded
. And automatically converted POST request methods, it can be omitted -X POST
.
-d
Local parameter data can be read text file is transmitted to the server.
$ curl -d '@data.txt' https://google.com/login
The above command to read data.txt
the contents of a file, sent to the server as the data thereof.
--data-urlencode
--data-urlencode
Equivalent parameters -d
, POST request body of data transmission, except that the data will automatically be transmitted URL encoding.
$ curl --data-urlencode 'comment=hello world' https://google.com/login
In the above code, the data transmitted hello world
has a space between, URL encoding is required.
-e
-e
Parameter sets the HTTP header Referer
indicating the source of the request.
curl -e 'https://google.com?q=example' https://www.example.com
The above command Referer
header is set https://google.com?q=example
.
-H
Parameters by directly adding headers Referer
to achieve the same effect.
curl -H 'Referer: https://google.com?q=example' https://www.example.com
-F
-F
Parameters used to upload binary files to the server.
$ curl -F '[email protected]' https://google.com/profile
The above command will add HTTP request header Content-Type: multipart/form-data
, then the file photo.png
as a file
field to upload.
-F
Parameters can be specified MIME type.
$ curl -F '[email protected];type=image/png' https://google.com/profile
The above command specifies the MIME type image/png
, otherwise it will curl MIME type is set application/octet-stream
.
-F
Parameters can also specify the file name.
$ curl -F '[email protected];filename=me.png' https://google.com/profile
The above command, the original file is named photo.png
, but the server receives a file named me.png
.
-G
-G
Parameters 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, URL is actually requested https://google.com/search?q=kitties&count=20
. If omitted --G
, it will issue a POST request.
If the URL-encoded data needs to be combined with --data--urlencode
parameters.
$ curl -G --data-urlencode 'comment=hello world' https://www.example.com
-H
-H
Parameters are added to the HTTP request header.
$ 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 two commands add HTTP headers.
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
The above command adds HTTP request headers are Content-Type: application/json
followed by -d
the transmission data parameters JSON.
-i
-i
Print out parameter server response HTTP headers.
$ curl -i https://www.example.com
After the above command server response is received, the first output of the server response headers, then a blank line, then the output source web.
-I
-I
HEAD request parameter sent, then the server will return an HTTP header to the print server.
$ curl -I https://www.example.com
The output of the above command server response to a HEAD request.
--head
Equivalent to parameters -I
.
$ curl --head https://www.example.com
-k
-k
SSL parameter specifies the skipped detection.
$ curl -k https://www.example.com
The above command does not check the server's SSL certificate is correct.
-L
-L
Parameters will follow HTTP server redirects requests. curl default does not follow redirects.
$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet
--limit-rate
--limit-rate
Used to limit the bandwidth of HTTP requests and responses to simulate slow speed environment.
$ curl --limit-rate 200k https://google.com
The above command will limit the bandwidth of 200K bytes per second.
-O
-o
Parameters will be saved into a file server to respond, equivalent to the wget
command.
$ curl -o example.html https://www.example.com
The above command will www.example.com
save as example.html
.
-O
-O
The server responds with parameters saved to a file, and the last part of the URL as the file name.
$ curl -O https://www.example.com/foo/bar.html
The above command will save the server to respond to a file named bar.html
.
-s
-s
Parameter will not output errors and progress information.
$ curl -s https://www.example.com
Once the above command error occurs, no error message is displayed. Error does not occur, it will display properly operating results.
If you want curl does not produce any output, you can use the following command.
$ curl -s -o /dev/null https://google.com
-S
-S
Parameter specifies that only output error messages, and usually -s
used together.
$ curl -s -o /dev/null https://google.com
The above command no output, unless an error occurs.
-u
-u
Parameters used to set the server authentication username and password.
$ curl -u 'bob:12345' https://google.com/login
The above command sets the user name bob
, password 12345
, and then into the HTTP header Authorization: Basic Ym9iOjEyMzQ1
.
curl inside the URL can identify a user name and password.
$ curl https://bob:12345@google.com/login
The above command can identify which URL of a username and password, which was converted to the inside of example HTTP header.
$ curl -u 'bob' https://google.com/login
The above command only set a user name, after the execution, curl will prompt the user to enter a password.
-v
-v
Output of the entire communication process parameters for debugging.
$ curl -v https://www.example.com
--trace
Parameters can also be used for debugging, but also outputs raw binary data.
$ curl --trace - https://www.example.com
-x
-x
Parameter specifies the proxy HTTP requests.
$ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com
HTTP request specified by the above command myproxy.com:8080
is issued socks5 proxy.
If you do not specify the agency agreement, the default is HTTP.
$ curl -x james:cats@myproxy.com:8080 https://www.example.com
The above command, the agent requests using the HTTP protocol.
-X
-X
The method of HTTP request specifies parameters.
$ curl -X POST https://www.example.com
The above command https://www.example.com
requests issued POST.
Reference links
(Finish)