curl使用
本文介绍curl工具的常见用法,使用 https://httpbin.org/ 这个网站作为 Http Server 进行测试
常见用法
最简单的GET请求
[root@localhost ~]# curl https://httpbin.org/get
{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1",
"X-Amzn-Trace-Id": "Root=1-697b7bb9-72d9720837c940ea3b545d06"
},
"origin": "223.104.83.109",
"url": "https://httpbin.org/get"
}
不同的请求方式,使用 -X 指定请求方法即可
# POST请求
curl -X POST https://httpbin.org/post
# PUT请求
curl -X PUT https://httpbin.org/put
# DELETE请求
curl -X DELETE https://httpbin.org/delete
带参数请求,请求即Payloads
# JSON: Content-Type
curl -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"username": "jdoe", "active": true}'
# 表单:application/x-www-form-urlencoded
curl -X POST https://httpbin.org/post \
-d "name=John+Doe" \
-d "email=john@example.com"
# 上传文件: multipart/form-data
curl -X POST https://httpbin.org/post -F "file=@localfile.txt"
# body比较大,不方便加在命令行,将POST body放进文件里读取
curl -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d @data.json
设置请求头
curl -H "X-My-Header: CoolValue" https://httpbin.org/headers
设置Cookies
curl -b "session_id=12345; preference=dark_mode" https://httpbin.org/cookies
# 访问接口,并将Cookies保存到文件
curl -c cookies.txt https://httpbin.org/cookies/set/mycookie/value
# 将Cookies保存到文件进行读取
curl -b cookies.txt https://httpbin.org/cookies
Windows 下的 curl
Windows 下PowerShell中的curl实际调用的是Invoke-WebRequest
https://stackoverflow.com/questions/47364244/curl-vs-invoke-webrequest

浙公网安备 33010602011771号