table of Contents
Brief introduction
curl is a command line tool, as the name suggests is the client's URL tool.
The tool is so powerful, as many as dozens of command-line arguments, fully comparable to the postman this type of graphical interface tools.
Documentation: https: //catonmat.net/cookbooks/curl
reference:
http://www.ruanyifeng.com/blog/2011/09/curl.html
https://www.ruanyifeng.com/blog/2019/09/curl-reference.html
GET
Send a GET request, and the results are printed out
curl https://catonmat.net
Send a GET request and response of the body to the output file
curl -o output.txt https://catonmat.net
POST
Sending an empty POST request
curl -X POST https://catonmat.net
POST request transmission parameters has
curl -d 'login=Queen&password=123' -X POST https://google.com/login
When a -d
parameter passed in the form of data, automatically Content-Type
set to application/x-www-form-urlencoded
the same time when using -d
the time parameter, -X POST
it may be omitted.
-d
Parameters can also be written separately:curl -d 'login=Queen' -d 'password' https://google.com/login
Transmission parameters may have a POST request redirection
curl -L -d 'tweet=hi' https://api.twitter.com/tweet
curl
Default does not support redirection, add -L
parameters can be explicitly requested any redirection.
POST request data with JSON
curl -d '{"login":"Queen","password":"123"}' -H 'Content-Type: application/json' https://google.com/login
Using the -d
parameter data passed JSON, and must use -H
the parameters specified explicitly Content-Type
asapplication/json
POST request data with XML
curl -d '<user><login>ann</login><password>123</password></user>' -H 'Content-Type: application/xml' https://google.com/login
Using -d
arguments passed xml data, and must use -H
the parameters specified explicitly Content-Type
asapplication/xml
POST request with the plaintext data
curl -d 'hello world' -H 'Content_Type: text/plain' https://google.com/login
Using -d
parameters passed in plain text data while using the -H
parameters specified display Content-Type
astext/plain
POST request with the data in a file
curl -d '@data.txt' https://google.com/login
Use -d
parameter incoming data, which @
later symbol indicating that the data from that file.
The explicit URL encoded POST data
curl --data-urlencode 'comment=hello 中国' https://google.com/login
Use -d
, the default data has been considered POST URL encoding , but if the data does not pass encoding process, then you need to use --data-urlencode
after the data has not been encoded URL encoding, and then send
POST a binary file
curl -F '[email protected]' https://google.com/profile
Use -F
parameters to force curl
to send a multi-part form data, automatically Content-Type
set multipart/form-data
, let the statement curl
read pig.png
data and upload it to https://google.com/profile
, and name the filenewfile
Transmitting a binary data, and set its MIME type
curl -F '[email protected];type=iamge/png'
Use -F
parameter upload a binary file, and set the MIME type of the file to image/png
, if not set, it defaults toapplication/octet-stream
MIME type (Multipurpose Internet Mail Extensions, media type) is a standard used to represent the nature and format of documents, files, and a stream of bytes.
Send binary data and change the file name
curl -F '[email protected];filename=cat.png' https://google.com/login
Like the previous two, the statement sent via a POST request binary data, and change the file name, so that the server can see it is not the original name pig.png
, but a new name cat.png
, and save it asnewfile