-o <file> # --output: 保存输出到本地文件
-u user:pass # --user: HTTP 身份认证账号密码
-v # --verbose: 详细输出模式 (显示请求头和响应头)
-vv # 更加详细的调试日志
-s # --silent: 静默模式,不显示进度条和错误
-S # --show-error: 与 --silent (-sS) 配合使用,隐藏进度条但显示错误
-i # --include: 在输出中包含 HTTP 响应头
-I # --head: 仅获取 HTTP 响应头 (HEAD 请求)
-X POST # --request: 指定 HTTP 方法
-L # --location: 如果页面发生 301/302 重定向,自动追踪重定向链接
-F # --form: 模拟表单 multipart/form-data 数据提交
# --data: HTTP POST 报文数据
# URL 编码形式 (如 status="Hello")
-d 'data'
# --data 从文件中读取数据内容
-d @file
# --get: 将 -d 的数据通过 GET 查询参数追加发送
-G
-A <str> # --user-agent: 设置客户端 User-Agent 字符串
-b name=val # --cookie: 传递单个 Cookie 键值对
-b, --cookie FILE # 从指定的 Cookie 文件中读取 Cookies
-c, --cookie-jar FILE # 将接收到的 Cookies 保存到指定文件
-H "X-Foo: y" # --header: 添加自定义 HTTP 请求头
--compressed # 请求使用 deflate/gzip 压缩响应
--cacert <file> # 指定 CA 证书文件
--capath <dir> # 指定 CA 证书目录路径
-E, --cert <cert> # --cert: 客户端证书文件
--cert-type # 证书格式类型 (der/pem/eng)
-k, --insecure # 允许不安全的 SSL 连接 (忽略自签名证书错误)
安装依赖
apk add --update curl # 在 Alpine Linux 中安装 curl
| 命令指令 | 含义说明 |
|---|---|
curl -I https://qr.warpnav.com |
仅发送 HEAD 请求获取响应头 |
curl -v -I https://qr.warpnav.com |
带详细过程日志的 HEAD 请求 |
curl -X GET https://qr.warpnav.com |
使用显式定义的 GET HTTP 方法 |
curl --noproxy 127.0.0.1 http://www.stackoverflow.com |
不使用 HTTP 代理发起请求 |
curl --connect-timeout 10 -I -k https://qr.warpnav.com |
设置 10 秒连接超时且忽略自签名证书 |
curl --verbose --header "Host: www.mytest.com:8182" qr.warpnav.com |
带自定义 Host 请求头的高级请求 |
curl -k -v https://www.google.com |
获取带响应头的详细调试数据 |
$ curl -v --include \
--form key1=value1 \
--form upload=@localfilename URL
$ curl -XGET http://${elasticsearch_ip}:9200/_cluster/nodes | python -m json.tool
| 命令指令 | 含义说明 |
|---|---|
curl -d "name=username&password=123456" <URL> |
发送表单序列化 POST 数据 |
curl <URL> -H "content-type: application/json" -d "{ \"woof\": \"bark\"}" |
发送 JSON 格式的 POST 报文 |
curl -sSL https://get.rvm.io | bash
| 命令指令 | 含义说明 |
|---|---|
curl -L -s http://ipecho.net/plain, curl -L -s http://whatismijnip.nl |
获取当前出口公网 IP 地址 |
curl -u $username:$password http://repo.dennyzhang.com/README.txt |
携带 HTTP 认证账号密码请求 |
curl -v -F key1=value1 -F upload=@localfilename <URL> |
模拟表单上传本地文件 |
curl -k -v --http2 https://www.google.com/ |
强制使用 HTTP/2 协议发起请求 |
curl -T cryptopp552.zip -u test:test ftp://10.32.99.187/ |
通过 FTP 协议上传文件 |
curl -u test:test ftp://10.32.99.187/cryptopp552.zip -o cryptopp552.zip |
通过 FTP 协议下载文件 |
curl -v -u admin:admin123 --upload-file package1.zip http://mysever:8081/dir/package1.zip |
携带凭证通过 HTTP PUT 上传文件 |
curl -s -w \
'\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nAppCon time:\t%{time_appconnect}\nRedirect time:\t%{time_redirect}\nPreXfer time:\t%{time_pretransfer }\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' \
-o /dev/null https://www.google.com
curl -o /dev/null --silent -Iw "%{http_code}" https://example.com/my.remote.tarball.gz
curl https://example.com | \
grep --only-matching 'src="[^"]*.[png]"' | \
cut -d \" -f2 | \
while read i; do curl https://example.com/"${i}" \
-o "${i##*/}"; done
使用 GNU grep 下载页面上的所有 PNG 图片文件。
curl --remote-name "https://example.com/linux-distro.iso"
重命名输出保存
curl --remote-name "http://example.com/index.html" --output foo.html
curl --remote-name --continue-at -"https://example.com/linux-distro.iso"
curl "https://www.{example,w3,iana}.org/index.html" --output "file_#1.html"
curl "https://{foo,bar}.com/file_[1-4].webp" --output "#1_#2.webp"
下载一系列连续编号的文件(输出为 foo_file1.webp, foo_file2.webp...bar_file1_webp 等)。
$ curl http://url/file > file
$ curl --user username:password http://example.com/
$ curl -u username:password http://example.com/
$ curl -o file http://url/file
$ curl --output file http://url/file
$ curl -I url
# 仅展示 Header 响应头
$ curl -o file http://url/file
$ curl --output file http://url/file
$ curl -s http://url/myscript.sh
curl -K file
# 从配置文件中读取参数
curl --config file
$HOME/.curlrc # 类 UNIX 系统上的默认用户配置文件