Administrator
发布于 2026-05-17 / 9 阅读
0
0

curl 命令完全使用指南

curl 命令完全使用指南

curl 是 Linux/macOS 下最强大的命令行 HTTP 客户端,支持 HTTP/HTTPS/FTP/SCP 等多种协议,是开发调试、接口测试的必备工具。


📦 安装 curl

# Debian/Ubuntu
sudo apt install curl

# CentOS/RHEL
sudo yum install curl

# macOS (通常已预装)
brew install curl

# 验证安装
curl --version

🚀 基础用法

GET 请求

最简单的用法,获取网页内容:

# 获取网页
curl https://example.com

# 保存到文件
curl -o output.html https://example.com

# 使用 -O 保留远程文件名
curl -O https://example.com/file.zip

查看详细信息

# 显示 HTTP 头信息
curl -I https://example.com

# 显示详细请求过程(调试用)
curl -v https://example.com

# 只显示 HTTP 状态码
curl -s -o /dev/null -w "%{http_code}" https://example.com

📮 POST 请求

发送 JSON 数据

# 使用 -d 发送 JSON
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name": "John", "email": "john@example.com"}'

# 从文件读取 JSON
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d @data.json

发送表单数据

# application/x-www-form-urlencoded
curl -X POST https://example.com/login \
  -d "username=admin&password=123456"

# multipart/form-data(文件上传)
curl -X POST https://example.com/upload \
  -F "file=@/path/to/file.jpg" \
  -F "description=My photo"

🔐 认证方式

Basic 认证

curl -u username:password https://api.example.com/data

# 或者
curl -H "Authorization: Basic $(echo -n 'user:pass' | base64)" https://api.example.com/data

Bearer Token

curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data

API Key

# 放在 Header 中
curl -H "X-API-Key: YOUR_API_KEY" https://api.example.com/data

# 放在 Query 参数中
curl "https://api.example.com/data?api_key=YOUR_API_KEY"

⚙️ 常用参数

请求控制

# 指定请求方法
curl -X PUT ...
curl -X DELETE ...
curl -X PATCH ...

# 设置请求头
curl -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "X-Custom-Header: value" \
     https://api.example.com

# 设置超时(秒)
curl --connect-timeout 5 \
     --max-time 30 \
     https://example.com

输出控制

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

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

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

# 显示进度条
curl -# -O https://example.com/large-file.zip

代理设置

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

# SOCKS5 代理
curl --socks5 127.0.0.1:1080 https://example.com

# 使用环境变量
export http_proxy=http://proxy:8080
export https_proxy=http://proxy:8080
curl https://example.com

🛠️ 实战场景

1. 测试 REST API

# GET - 获取列表
curl -s https://api.example.com/users | jq .

# POST - 创建资源
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice", "role": "admin"}'

# PUT - 更新资源
curl -X PUT https://api.example.com/users/1 \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice", "role": "superadmin"}'

# DELETE - 删除资源
curl -X DELETE https://api.example.com/users/1

2. 下载文件

# 下载并显示进度
curl -# -O https://example.com/linux.iso

# 断点续传
curl -C - -O https://example.com/large-file.zip

# 限速下载(1MB/s)
curl --limit-rate 1M -O https://example.com/file.zip

3. 调试接口

# 查看完整请求和响应
curl -v https://api.example.com/data 2>&1 | head -50

# 只看响应头
curl -sI https://api.example.com/data

# 查看 TLS/SSL 信息
curl -v --insecure https://self-signed.example.com 2>&1 | grep -i ssl

4. 模拟浏览器请求

curl -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0" \
     -H "Accept: text/html,application/xhtml+xml" \
     -H "Accept-Language: zh-CN,zh;q=0.9" \
     -L https://example.com

5. 并发请求(压测)

# 使用 xargs 并发 10 个请求
seq 1 10 | xargs -P 10 -I {} curl -s https://api.example.com/data > /dev/null

# 使用 GNU parallel
seq 1 100 | parallel -j 10 curl -s https://api.example.com/data > /dev/null

📋 实用技巧

使用配置文件

创建 ~/.curlrc

# 默认跟随重定向
-L
# 默认静默模式
-s
# 默认显示错误
-S

保存和复用 Cookie

# 保存 Cookie
curl -c cookies.txt https://example.com/login \
  -d "user=admin&pass=123"

# 复用 Cookie
curl -b cookies.txt https://example.com/dashboard

发送 JSON 文件

# 方法1:使用 @
curl -X POST https://api.example.com/data \
  -H "Content-Type: application/json" \
  -d @payload.json

# 方法2:使用 cat
curl -X POST https://api.example.com/data \
  -H "Content-Type: application/json" \
  -d "$(cat payload.json)"

格式化 JSON 输出

# 使用 jq 格式化
curl -s https://api.example.com/data | jq .

# 使用 python
curl -s https://api.example.com/data | python3 -m json.tool

🆚 curl vs wget

特性curlwget
主要用途API 测试、数据传输文件下载
输出默认输出到 stdout默认保存文件
POST 支持✅ 完整支持❌ 有限支持
递归下载❌ 不支持✅ 支持
断点续传-C --c

📚 速查表

# GET 请求
curl https://api.example.com/data

# POST JSON
curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' URL

# 带认证
curl -u user:pass URL
curl -H "Authorization: Bearer TOKEN" URL

# 下载文件
curl -O URL
curl -o filename URL

# 上传文件
curl -F "file=@file.txt" URL

# 显示头信息
curl -I URL

# 跟随重定向
curl -L URL

# 静默模式
curl -s URL

# 调试模式
curl -v URL

💡 小贴士

  1. 生产环境慎用 --insecure:跳过 SSL 验证有安全风险
  2. 敏感信息用变量:不要在命令行硬编码密码
    curl -H "Authorization: Bearer $API_TOKEN" URL
    
  3. 大响应用 jq 处理:配合 jq 过滤需要的字段
  4. 善用 -w 自定义输出:可以输出时间、状态码等信息

掌握了 curl,你就掌握了与 HTTP 世界交互的万能钥匙。 🔑


评论