强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

curl 深度教程 / 第 03 章:基本用法

第 03 章:基本用法

本章是 curl 的"Hello World"——掌握这些基本操作后,你就能覆盖日常开发中 80% 的 curl 使用场景。


3.1 最简单的请求

GET 请求

# 最简单的 GET 请求(输出到 stdout)
curl https://httpbin.org/get

# 带有查询参数的 GET 请求
curl "https://httpbin.org/get?name=curl&version=8"

# 使用 --data-urlencode 自动编码参数
curl -G https://httpbin.org/get \
  --data-urlencode "name=张三" \
  --data-urlencode "city=北京"

# 等价于:
# curl "https://httpbin.org/get?name=%E5%BC%A0%E4%B8%89&city=%E5%8C%97%E4%BA%AC"

输出到文件

# 使用 -o 指定文件名
curl -o output.html https://example.com

# 使用 -O 使用远程文件名
curl -O https://example.com/document.pdf

# 使用 -o 保存到指定目录
curl -o /tmp/page.html https://example.com

# 静默模式 + 保存文件(适合脚本)
curl -s -o /dev/null https://example.com

控制输出行为

# 默认行为:输出到 stdout
curl https://example.com

# 静默模式:不显示进度条和错误信息
curl -s https://example.com

# 仅显示错误(适合脚本中使用)
curl -S https://example.com

# 静默但显示错误
curl -sS https://example.com

# 不输出响应体(仅获取头信息)
curl -I https://example.com

# 同时输出响应头和响应体
curl -i https://example.com

# 详细模式(显示请求/响应细节)
curl -v https://example.com

3.2 发送 POST 请求

基本 POST

# 发送 POST 请求(application/x-www-form-urlencoded)
curl -X POST https://httpbin.org/post \
  -d "name=curl&version=8"

# 等价于(-X POST 可省略,-d 自动设置 POST 方法)
curl https://httpbin.org/post \
  -d "name=curl&version=8"

# 发送 JSON POST
curl -X POST https://httpbin.org/post \
  -H "Content-Type: application/json" \
  -d '{"name": "curl", "version": 8}'

# 从文件读取 POST 数据
curl -X POST https://httpbin.org/post \
  -H "Content-Type: application/json" \
  -d @data.json

# 从 stdin 读取 POST 数据
echo '{"key": "value"}' | curl -X POST https://httpbin.org/post \
  -H "Content-Type: application/json" \
  -d @-

发送原始数据

# 使用 --data-raw 发送原始数据(不处理 @ 前缀)
curl https://httpbin.org/post \
  --data-raw '{"key": "@value"}'

# 使用 --data-binary 发送二进制数据
curl https://httpbin.org/post \
  --data-binary @image.png

# 使用 --data-urlencode 发送 URL 编码数据
curl https://httpbin.org/post \
  --data-urlencode "comment=Hello World&Goodbye"

3.3 请求头管理

设置请求头

# 设置单个请求头
curl -H "User-Agent: MyApp/1.0" https://example.com

# 设置多个请求头
curl -H "User-Agent: MyApp/1.0" \
     -H "Accept: application/json" \
     -H "X-Request-ID: abc123" \
     https://api.example.com/data

# 移除默认请求头(使用分号后无值)
curl -H "User-Agent:" https://example.com

# 设置 Cookie
curl -H "Cookie: session=abc123; user=张三" https://example.com

# 使用 -b 设置 Cookie
curl -b "session=abc123" https://example.com

# 使用 Cookie 文件
curl -b cookies.txt https://example.com

查看响应头

# 仅查看响应头
curl -I https://example.com

# 查看响应头和响应体
curl -i https://example.com

# 详细模式查看所有头信息
curl -v https://example.com 2>&1 | grep "^[<>]"

# 提取特定响应头
curl -sI https://example.com | grep -i "content-type"

3.4 输出控制

输出格式

# 原始输出(默认)
curl https://httpbin.org/get

# 美化 JSON 输出(需要 jq)
curl -s https://httpbin.org/get | jq .

# 仅提取特定字段
curl -s https://httpbin.org/get | jq '.headers["User-Agent"]'

# 输出为 HTML 格式(在终端中显示)
curl -s https://example.com | lynx -stdin

# 输出到多个文件
curl -o header.txt -D - https://example.com > body.txt

写入响应头

# 使用 -D 将响应头保存到文件
curl -D response_headers.txt https://example.com

# 使用 -D 将响应头输出到 stdout
curl -D - https://example.com

# 结合 -o 和 -D 分别保存响应体和响应头
curl -o body.html -D headers.txt https://example.com

进度条控制

# 显示进度条(默认,当输出到终端时)
curl -O https://example.com/largefile.zip

# 显示简化的进度条
curl -# -O https://example.com/largefile.zip

# 静默模式(不显示进度条)
curl -s -O https://example.com/largefile.zip

# 强制显示进度条(即使输出被重定向)
curl -O https://example.com/largefile.zip --progress-bar

3.5 URL 处理

URL 编码

# 手动编码 URL
curl "https://httpbin.org/get?name=%E5%BC%A0%E4%B8%89"

# 使用 --data-urlencode 自动编码
curl -G https://httpbin.org/get \
  --data-urlencode "name=张三"

# 多个参数
curl -G https://httpbin.org/get \
  --data-urlencode "name=张三" \
  --data-urlencode "city=北京" \
  --data-urlencode "message=Hello World"

URL 变量

# 使用 {} 展开多个 URL
curl "https://httpbin.org/{get,post,put}"

# 使用 [] 生成数字序列
curl "https://httpbin.org/get?page=[1-5]"

# 使用 [] 生成字母序列
curl "https://httpbin.org/get?file=[a-c].txt"

# 组合使用
curl "https://httpbin.org/get?id=[1-3]&type=[a,b,c]"

代理和重定向

# 跟随重定向
curl -L https://example.com

# 限制重定向次数
curl -L --max-redirs 5 https://example.com

# 使用代理
curl -x http://proxy.example.com:8080 https://example.com

# 使用 SOCKS 代理
curl --socks5 127.0.0.1:1080 https://example.com

3.6 常用选项速查表

选项简写说明
--url指定 URL
--request-X指定 HTTP 方法
--header-H设置请求头
--data-d发送 POST 数据
--data-raw发送原始 POST 数据
--data-binary发送二进制数据
--data-urlencode发送 URL 编码数据
--get-G将 -d 数据转为查询参数
--output-o输出到文件
--remote-name-O使用远程文件名保存
--include-i输出响应头
--head-I仅获取头信息
--verbose-v详细模式
--silent-s静默模式
--show-error-S显示错误信息
--location-L跟随重定向
--cookie-b设置 Cookie
--cookie-jar-c保存 Cookie
--user-agent-A设置 User-Agent
--referer-e设置 Referer
--compressed请求压缩响应
--connect-timeout连接超时(秒)
--max-time最大传输时间(秒)
--retry失败重试次数
--retry-delay重试间隔(秒)
--max-redirs最大重定向次数
--insecure-k忽略 SSL 证书错误
--cacert指定 CA 证书文件
--cert-E指定客户端证书
--key指定客户端私钥
--proxy-x使用代理
--user-u设置认证信息
--http1.1强制使用 HTTP/1.1
--http2强制使用 HTTP/2
--http3强制使用 HTTP/3

3.7 实用组合示例

示例 1:API 调试

# 完整的 API 调试命令
curl -v -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"name": "张三", "email": "zhangsan@example.com"}' \
  -w "\n\n--- 调试信息 ---\nHTTP 状态码: %{http_code}\n响应时间: %{time_total}s\n大小: %{size_download} bytes\n"

示例 2:批量下载

# 下载多个文件
for i in $(seq 1 10); do
  curl -s -O "https://example.com/files/file${i}.txt"
done

示例 3:检查服务状态

# 检查服务健康状态
if curl -sf http://localhost:8080/health > /dev/null; then
  echo "✅ 服务正常运行"
else
  echo "❌ 服务异常"
  exit 1
fi

注意事项

  1. 引号很重要:URL 中的特殊字符(&?=)必须用引号包裹
  2. -d 自动设置 POST:使用 -d 时不需要显式指定 -X POST
  3. 输出顺序-o-O 之前时会影响文件名
  4. 编码问题:中文参数需要 URL 编码
  5. Cookie 会话:使用 -b-c 可以维持会话状态
# ❌ 错误:& 被 shell 解释
curl https://api.example.com/data?page=1&size=10

# ✅ 正确:用引号包裹
curl "https://api.example.com/data?page=1&size=10"

扩展阅读


📖 下一章第 04 章:HTTP 方法详解 — 深入了解 GET/POST/PUT/DELETE/PATCH/OPTIONS/HEAD 方法的使用和区别。