🚀 基础请求 >>>
curl 最核心的 HTTP 请求功能
curl <url>:发起 GET 请求curl -X POST <url>:指定请求方法curl -X PUT <url>:发送 PUT 请求curl -X DELETE <url>:发送 DELETE 请求curl -X PATCH <url>:发送 PATCH 请求curl -I <url>:仅获取响应头curl -i <url>:显示响应头和响应体curl -v <url>:显示详细通信过程curl -s <url>:静默模式(不显示进度)curl -L <url>:跟随重定向curl --max-redirs <n> <url>:限制重定向次数
# 基本 GET 请求
curl https://api.example.com/users
# 跟随重定向并显示响应头
curl -iL https://httpbin.org/redirect/1
# 静默模式获取纯响应内容
curl -s https://api.example.com/data | jq .
📤 发送数据 >>>
使用 curl 发送各种格式的数据
curl -d "data" <url>:发送 POST 数据curl -d @file.json <url>:从文件读取数据curl --data-urlencode "key=value" <url>:URL 编码数据curl -F "name=value" <url>:发送表单数据curl -F "file=@path/to/file" <url>:上传文件curl -T file.txt <url>:直接上传文件内容curl --json '{"key":"val"}' <url>:发送 JSON 数据 (8.5+)
# 发送表单数据
curl -X POST -d "name=John&age=30" https://api.example.com/users
# 发送 JSON 数据
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"John","age":30}' \
https://api.example.com/users
# 从文件读取 JSON 数据
curl -X POST \
-H "Content-Type: application/json" \
-d @user.json \
https://api.example.com/users
# 上传文件
curl -F "file=@photo.jpg" \
-F "description=My photo" \
https://api.example.com/upload
# 使用 --json 简化 JSON 请求 (curl 8.5+)
curl --json '{"name":"John"}' https://api.example.com/users
📝 请求头设置 >>>
自定义 HTTP 请求头
curl -H "Header: Value" <url>:添加自定义请求头curl -A "User-Agent" <url>:设置 User-Agentcurl -e "referer" <url>:设置 Referercurl -b "cookie=data" <url>:发送 Cookiecurl -c cookies.txt <url>:保存 Cookie 到文件curl -b cookies.txt <url>:从文件读取 Cookie
# 设置多个请求头
curl -H "Authorization: Bearer token123" \
-H "Accept: application/json" \
-H "X-Custom-Header: value" \
https://api.example.com/data
# 设置 User-Agent
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" \
https://example.com
# 保存和复用 Cookie
curl -c cookies.txt https://example.com/login
curl -b cookies.txt https://example.com/profile
💾 输出控制 >>>
控制 curl 的输出行为
curl -o file.txt <url>:保存响应到指定文件curl -O <url>:使用远程文件名保存curl -C - -o file.txt <url>:断点续传curl -# <url>:显示简单进度条curl --progress-bar <url>:强制显示进度条curl --create-dirs <url>:自动创建目录
# 保存到指定文件
curl -o output.json https://api.example.com/data
# 使用远程原始文件名
curl -O https://example.com/files/document.pdf
# 断点续传下载大文件
curl -C - -O https://example.com/large-file.zip
# 下载到指定目录(自动创建)
curl --create-dirs -o downloads/file.txt https://example.com/file.txt
🔐 认证授权 >>>
各种认证方式
curl -u user:pass <url>:HTTP Basic 认证curl --digest -u user:pass <url>:Digest 认证curl --ntlm -u user:pass <url>:NTLM 认证curl --negotiate -u : <url>:SPNEGO/Kerberoscurl --oauth2-bearer token <url>:OAuth2 Bearer Tokencurl -E cert.pem <url>:使用客户端证书
# Basic 认证
curl -u username:password https://api.example.com/private
# 交互式输入密码(更安全)
curl -u username https://api.example.com/private
# Bearer Token 认证
curl -H "Authorization: Bearer <token>" https://api.example.com/data
# 使用客户端证书
curl -E client.pem:password \
--cacert ca.pem \
https://secure.example.com/api
# 跳过证书验证(仅开发使用)
curl -k https://self-signed.example.com
⚡ 高级选项 >>>
高级功能和性能优化
curl --connect-timeout <s> <url>:连接超时curl --max-time <s> <url>:最大传输时间curl --retry <n> <url>:失败重试次数curl --retry-delay <s> <url>:重试间隔curl --limit-rate <speed> <url>:限速下载curl -Y <speed> <url>:最小下载速度curl --compressed <url>:请求压缩响应curl --http2 <url>:强制使用 HTTP/2curl --http3 <url>:尝试使用 HTTP/3
# 设置超时和重试
curl --connect-timeout 10 \
--max-time 30 \
--retry 3 \
--retry-delay 2 \
https://api.example.com/data
# 限速下载(避免占用全部带宽)
curl --limit-rate 500K -O https://example.com/large-file.zip
# 请求压缩响应
curl --compressed https://api.example.com/data
# 强制 HTTP/2
curl --http2 https://example.com
🌐 代理设置 >>>
配置代理服务器
curl -x proxy:port <url>:使用 HTTP 代理curl -x socks5://proxy:port <url>:使用 SOCKS5 代理curl --proxy-user user:pass <url>:代理认证curl --proxy-basic <url>:Basic 代理认证curl --proxy-digest <url>:Digest 代理认证curl --noproxy "*" <url>:禁用代理
# HTTP 代理
curl -x http://proxy.example.com:8080 https://api.example.com
# SOCKS5 代理
curl -x socks5://localhost:1080 https://api.example.com
# 代理认证
curl -x http://proxy.example.com:8080 \
--proxy-user username:password \
https://api.example.com
# 特定主机不走代理
curl -x http://proxy:8080 --noproxy "localhost,*.local" \
https://api.example.com
📊 调试诊断 >>>
调试和诊断网络问题
curl -v <url>:详细模式curl --trace - <url>:完整跟踪输出到 stdoutcurl --trace trace.txt <url>:完整跟踪输出到文件curl --trace-ascii - <url>:ASCII 格式跟踪curl --write-out "%{http_code}" <url>:自定义输出格式curl -w "@format.txt" <url>:从文件读取输出格式
# 详细模式查看请求和响应头
curl -v https://api.example.com/users
# 查看 HTTP 状态码和时间信息
curl -w "\nHTTP Code: %{http_code}\nTime: %{time_total}s\n" \
https://api.example.com
# 完整跟踪保存到文件
curl --trace debug.log https://api.example.com
# 仅获取状态码
curl -s -o /dev/null -w "%{http_code}" https://api.example.com
# 获取详细的连接信息
curl -w @- -o /dev/null -s https://api.example.com <<'EOF'
DNS: %{time_namelookup}s
Connect: %{time_connect}s
SSL: %{time_appconnect}s
Total: %{time_total}s
EOF
🔧 FTP/SFTP 操作 >>>
FTP 和 SFTP 文件传输
curl ftp://user:pass@host/file:下载 FTP 文件curl -T file ftp://user:pass@host/path/:上传文件到 FTPcurl -l ftp://user:pass@host/:列出 FTP 目录curl -Q "command" ftp://...:发送 FTP 命令curl sftp://user@host/file:SFTP 下载curl --ftp-create-dirs -T file ftp://...:自动创建目录
# FTP 下载文件
curl ftp://username:password@ftp.example.com/file.txt -o file.txt
# FTP 上传文件
curl -T localfile.txt ftp://username:password@ftp.example.com/remote/
# 列出 FTP 目录
curl -l ftp://username:password@ftp.example.com/
# SFTP 下载
curl sftp://username@example.com/path/to/file.txt -o file.txt
# FTP 被动模式(解决防火墙问题)
curl --ftp-pasv ftp://username:password@ftp.example.com/file.txt
🛡️ SSL/TLS 配置 >>>
SSL/TLS 证书和加密配置
curl -k <url>:跳过 SSL 证书验证curl --cacert ca.pem <url>:指定 CA 证书curl --cert client.pem <url>:使用客户端证书curl --cert-type P12 <url>:指定证书类型curl --key key.pem <url>:指定私钥curl --ciphers <list> <url>:指定加密套件curl --tlsv1.2 <url>:强制 TLS 1.2curl --tls-max 1.2 <url>:设置最大 TLS 版本
# 使用自签名证书(开发环境)
curl -k https://self-signed.example.com
# 指定 CA 证书验证
curl --cacert /path/to/ca-bundle.crt \
https://secure.example.com
# 使用客户端证书
curl --cert client.crt --key client.key \
https://secure.example.com/api
# 使用 P12 证书
curl --cert client.p12:password \
--cert-type P12 \
https://secure.example.com
# 强制 TLS 1.3
curl --tlsv1.3 https://secure.example.com
📋 实用技巧 >>>
日常开发中的实用技巧
常用组合
curl -sL -o file <url>curl -s -o /dev/null -w "%{http_code}" <url>curl -# -O <url>curl -s -H "Accept: application/json" <url> | jq .
性能测试
# 测试响应时间
for i in {1..10}; do
curl -s -o /dev/null -w "%{time_total}\n" https://api.example.com
done
# 并行请求测试
seq 1 10 | xargs -n1 -P10 -I{} \
curl -s -o /dev/null -w "%{http_code}\n" https://api.example.com
与 jq 配合处理 JSON
# 格式化 JSON 响应
curl -s https://api.example.com/users | jq .
# 提取特定字段
curl -s https://api.example.com/users | jq '.[0].name'
# 遍历数组
curl -s https://api.example.com/users | jq '.[] | {id, name}'
保存会话 Cookie
# 登录并保存 Cookie
curl -c cookies.txt \
-d "username=admin&password=secret" \
https://example.com/login
# 使用 Cookie 访问受保护资源
curl -b cookies.txt https://example.com/dashboard
🌟 新特性 (curl 8.x) >>>
curl 8.x 版本的新功能
curl --json '<json>' <url>:简化 JSON 请求 (8.5.0+)curl --url-query "key=value" <url>:URL 查询参数 (8.0.0+)curl --rate <speed>:请求速率限制 (8.0.0+)curl --proxy-http2:HTTP/2 代理支持curl --http3-only:仅使用 HTTP/3
# 简化 JSON POST 请求 (8.5+)
curl --json '{"name":"test","value":123}' \
https://api.example.com/data
# URL 查询参数(自动编码)
curl --url-query "search=hello world" \
--url-query "page=1" \
https://api.example.com/search
# 限制请求速率(每秒 2 个请求)
curl --rate 2/s https://api.example.com/data[1-10]