curl 是一个功能强大的命令行工具,用于传输数据,支持多种协议(HTTP、HTTPS、FTP、FTPS、SCP、SFTP等)。以下是 curl 的主要参数和使用方法。
curl [options] [URL...]
-X
或 --request
: 指定 HTTP 请求方法 (GET, POST, PUT, DELETE 等)
curl -X POST https://example.com/api
-H
或 --header
: 添加 HTTP 请求头
curl -H "Content-Type: application/json" https://example.com
-d
或 --data
: 发送 POST 请求的数据
curl -d "name=John&age=30" https://example.com
--data-binary
: 发送二进制数据
curl --data-binary @file.txt https://example.com
--data-urlencode
: URL 编码发送的数据
curl --data-urlencode "name=John Doe" https://example.com
-G
或 --get
: 将 -d 数据作为 URL 查询参数发送 (GET 请求)
curl -G -d "q=keyword" https://example.com/search
-o
或 --output
: 将输出写入文件
curl -o output.html https://example.com
-O
或 --remote-name
: 使用远程文件名保存
curl -O https://example.com/file.zip
-s
或 --silent
: 静默模式 (不显示进度和错误信息)
curl -s https://example.com
-v
或 --verbose
: 显示详细操作信息
curl -v https://example.com
--trace
: 显示完整的跟踪信息
curl --trace trace.txt https://example.com
-u
或 --user
: 指定用户名和密码
curl -u username:password https://example.com
--basic
: 强制使用基本认证
curl --basic -u username:password https://example.com
--digest
: 使用摘要认证
curl --digest -u username:password https://example.com
-x
或 --proxy
: 使用代理服务器
curl -x http://proxy.example.com:8080 https://example.com
--proxy-user
: 代理认证
curl --proxy-user user:pass -x http://proxy.example.com https://example.com
--connect-timeout
: 连接超时时间 (秒)
curl --connect-timeout 30 https://example.com
-m
或 --max-time
: 最大传输时间 (秒)
curl -m 60 https://example.com
--retry
: 失败重试次数
curl --retry 3 https://example.com
-T
或 --upload-file
: 上传文件
curl -T localfile.txt ftp://example.com/
-C
或 --continue-at
: 断点续传
curl -C - -O https://example.com/bigfile.zip
-L
或 --location
: 跟随重定向
curl -L https://example.com
-k
或 --insecure
: 允许不安全的 SSL 连接
curl -k https://self-signed.example.com
--cacert
: 指定 CA 证书
curl --cacert /path/to/cert.pem https://example.com
-I
或 --head
: 只获取 HTTP 头信息
curl -I https://example.com
-A
或 --user-agent
: 设置 User-Agent
curl -A "Mozilla/5.0" https://example.com
-e
或 --referer
: 设置 Referer
curl -e "https://google.com" https://example.com
发送 JSON 数据
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' https://example.com/api
下载文件并显示进度
curl -# -O https://example.com/file.zip
保存 cookies 并在后续请求中使用
curl -c cookies.txt https://example.com/login
curl -b cookies.txt https://example.com/dashboard
上传多个文件
curl -F "file=@file1.txt" -F "file=@file2.txt" https://example.com/upload
测试 API 端点
curl -X GET "https://api.example.com/users?id=123" -H "Authorization: Bearer token123"
限制下载速度
curl --limit-rate 100K -O https://example.com/bigfile.iso
使用 HTTP/2
curl --http2 https://example.com
获取 HTTP 头信息
curl -I https://example.com
要查看 curl 的完整参数列表,可以使用 man curl
或 curl --help
命令。