curl用法指南
目录
1、常用指令
1.1、指定请求方式
-X, --request <command>
(HTTP) Specifies a custom request method to use when communicating with the HTTP
server. The specified request will be used instead of the method otherwise used
(which defaults to GET). Read the HTTP 1.1 specification for details and explana‐
tions. Common additional HTTP requests include PUT and DELETE, but related tech‐
nologies like WebDAV offers PROPFIND, COPY, MOVE and more.
Normally you don't need this option. All sorts of GET, HEAD, POST and PUT requests
are rather invoked by using dedicated command line options.
This option only changes the actual word used in the HTTP request, it does not
alter the way curl behaves. So for example if you want to make a proper HEAD
request, using -X HEAD will not suffice. You need to use the -I, --head option.
(FTP) Specifies a custom FTP command to use instead of LIST when doing file lists
with FTP.
If this option is used several times, the last one will be used.
使用--request或者简写-X,后面跟请求方式GET、POST、PUT、DELETE等。
1.2、指定请头
-H, --header <header>
(HTTP) Extra header to use when getting a web page. You may specify any number of
extra headers. Note that if you should add a custom header that has the same name
as one of the internal ones curl would use, your externally set header will be used
instead of the internal one. This allows you to make even trickier stuff than curl
would normally do. You should not replace internally set headers without knowing
perfectly well what you're doing. Remove an internal header by giving a replacement
without content on the right side of the colon, as in: -H "Host:". If you send the
custom header with no-value then its header must be terminated with a semicolon,
such as -H "X-Custom-Header;" to send "X-Custom-Header:".
curl will make sure that each header you add/replace is sent with the proper end-
of-line marker, you should thus not add that as a part of the header content: do
not add newlines or carriage returns, they will only mess things up for you.
See also the -A, --user-agent and -e, --referer options.
This option can be used multiple times to add/replace/remove multiple headers.
使用--header或者简写-H。
2、示例
2.1、使用GET请求
curl --request GET 'https://www.baidu.com?id=1' \
--header 'Accept: application/json'
2.2、使用POST请求发送表单数据
curl --request POST 'https://www.baidu.com' \
--header 'Accept: application/json' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'name=Jobs' \
--data-urlencode 'age=50'
2.3、使用POST请求上传文件
curl --request POST 'https://www.baidu.com' \
--header 'Accept: application/json' \
--header 'Content-Type: multipart/form-data' \
--form 'name="Jobs"' \
--form 'age="50"' \
--form 'file=@"/E:/demo.xls"'
2.4、使用POST请求发送JSON数据
curl --request POST 'https://www.baidu.com' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Jobs",
"age": 50
}'
2.5、使用POST请求发送XML数据
curl --request POST 'https://www.baidu.com' \
--header 'Accept: application/xml' \
--header 'Content-Type: application/xml' \
--data-raw '<data>
<name>Jobs</name>
<age>90</age>
</data>'
2.6、使用PUT请求发送JSON数据
curl --request PUT 'https://www.baidu.com' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Jobs",
"age": 50,
"id": 1
}'
2.7、使用DELETE请求
curl --request DELETE 'https://www.baidu.com?id=1' \
--header 'Accept: application/json'
3、相关资料
-----------------------------------------------------------------------------------------------------------
薔薇猛虎皆成個性,陽光雨露俱是天恩!
薔薇猛虎皆成個性,陽光雨露俱是天恩!