Skip to content

curl 命令操作详尽指南

约 1366 字大约 5 分钟

2026-02-12

curl 是一个强大的命令行工具,用于在服务器之间传输数据。它支持多种协议(HTTP、HTTPS、FTP、SFTP、LDAP 等),尤其擅长发送和接收 HTTP/HTTPS 请求

适用对象:开发者、运维工程师、测试人员、自动化脚本编写者 目标:掌握 curl 的核心用法,高效调试 API、下载文件、模拟请求


一、什么是 curl?

curl 是一个强大的命令行工具,用于在服务器之间传输数据。它支持多种协议(HTTP、HTTPS、FTP、SFTP、LDAP 等),尤其擅长发送和接收 HTTP/HTTPS 请求

  • 调试 RESTful API
  • 自动化文件下载/上传
  • 模拟浏览器行为
  • ✅ 测试接口认证、重定向、超时等场景

💡 提示:熟练使用 curl 可临时替代 apifox、Postman 等图形化工具进行快速测试!


二、安装 curl

大多数现代操作系统已预装 curl。若未安装,请按以下方式操作:

系统安装命令
Ubuntu/Debiansudo apt install curl
CentOS/RHELsudo yum install curlsudo dnf install curl
macOS已内置;若缺失,运行 brew install curl
Windows 10+已内置;旧版请从 curl.se/download 下载

验证安装:

curl --version

三、基础用法:GET 请求

默认行为就是 GET

curl https://httpbin.org/get

说明:不加任何参数时,curl 默认发送 GET 请求,并将响应体输出到终端。

保存响应到文件

curl -o response.json https://api.example.com/data

选项-o filename 将输出写入指定文件(覆盖模式) 等价写法curl https://... > response.json


四、常见 HTTP 方法操作

1️⃣ POST 请求(提交表单数据)

curl -X POST -d "username=admin&password=123456" https://api.example.com/login

说明

  • -X POST:显式指定请求方法为 POST
  • -d:发送 URL 编码的表单数据(Content-Type 默认为 application/x-www-form-urlencoded

2️⃣ POST JSON 数据(最常用!)

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"张三","age":28}' \
  https://api.example.com/users

关键点

  • 必须手动设置 -H "Content-Type: application/json"
  • -d 后跟合法 JSON 字符串(建议用单引号包裹)

3️⃣ PUT / PATCH(更新资源)

# 完整替换
curl -X PUT -H "Content-Type: application/json" -d '{"status":"active"}' https://api.example.com/users/123

# 部分更新(PATCH)
curl -X PATCH -H "Content-Type: application/json" -d '{"email":"new@example.com"}' https://api.example.com/users/123

4️⃣ DELETE(删除资源)

curl -X DELETE https://api.example.com/users/123

五、常用选项与技巧

设置请求头(Headers)

curl -H "Authorization: Bearer abc123xyz" \
     -H "User-Agent: MyApp/1.0" \
     https://api.example.com/profile

用途:传递 Token、自定义 UA、指定 Accept 类型等。


跟随重定向(3xx)

curl -L https://example.com/old-page

说明:默认 curl 不跟随重定向,加 -L 后自动跳转。


显示详细请求/响应过程(调试必备!)

curl -v https://api.example.com/test

输出内容

  • 请求行、请求头
  • 响应状态码、响应头
  • 连接细节(DNS、TCP、SSL 握手等)

仅获取响应头(HEAD 请求)

curl -I https://www.baidu.com

等价于curl -X HEAD -i https://...用途:检查资源是否存在、获取 Content-Length、Last-Modified 等元信息。


设置超时时间

# 连接超时 5 秒,总请求超时 10 
curl --connect-timeout 5 --max-time 10 https://slow-api.com

上传文件(multipart/form-data)

curl -X POST -F "file=@/path/to/photo.jpg" https://api.example.com/upload

说明

  • -F 表示使用 multipart/form-data 格式
  • @ 后跟本地文件路径

六、认证与安全

Basic 认证

curl -u username:password https://protected.example.com

等价写法curl -H "Authorization: Basic $(echo -n 'user:pass' | base64)" ...


Bearer Token(JWT/OAuth2)

curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." https://api.example.com/me

🔒 忽略 SSL 证书错误(仅测试环境!)

curl -k https://self-signed.badssl.com

⚠️ 警告:生产环境切勿使用 -k


七、Cookie 与会话管理

curl -c cookies.txt https://login.example.com
curl -b cookies.txt https://dashboard.example.com

说明

  • -c:保存响应中的 Set-Cookie 到文件
  • -b:发送请求时携带 Cookie 文件中的值

八、性能与调试

显示请求耗时

curl -w "总耗时: %{time_total}s\n响应码: %{http_code}\n" \
     -o /dev/null -s \
     https://api.example.com

常用变量

  • %{time_total}:总时间(秒)
  • %{http_code}:HTTP 状态码
  • %{size_download}:下载字节数

遇到 HTTP 错误时返回非零退出码

curl -f https://api.example.com/error || echo "请求失败!"

说明-f 选项使 curl 在收到 4xx/5xx 响应时返回非 0 退出码,便于脚本判断。


九、综合实战案例

案例:完整登录 + 获取用户信息

# 1. 登录并保存 Cookie
curl -c auth.cookie \
     -d "username=admin&password=secret" \
     https://api.example.com/login

# 2. 使用 Cookie 获取用户资料
curl -b auth.cookie \
     https://api.example.com/profile

案例:带 Token 的 JSON POST 并记录耗时

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(cat token.txt)" \
  -d '{"title":"新任务","priority":"high"}' \
  -w "耗时: %{time_total}s | 状态: %{http_code}\n" \
  -o task_response.json \
  https://api.example.com/tasks

十、常见问题排查

问题解决方案
返回 400 Bad Request检查 JSON 格式是否合法、Content-Type 是否正确
返回 401 Unauthorized检查 Token 是否过期、Header 是否拼写错误
连接超时--connect-timeout--max-time,或检查网络/防火墙
中文乱码添加 -H "Accept-Charset: utf-8" 或终端设置 UTF-8 编码
无法上传文件确认路径正确,使用 -F "file=@./filename"(注意 @

十一、附录:速查表

功能命令
GET 请求curl https://url
POST JSONcurl -X POST -H "Content-Type: application/json" -d '{}' url
带 Tokencurl -H "Authorization: Bearer xxx" url
保存响应curl -o file.txt url
调试详情curl -v url
跟随重定向curl -L url
显示耗时curl -w "%{time_total}s" -o /dev/null -s url

结语

curl 是每个开发者都应掌握的“瑞士军刀”。通过灵活组合选项,你可以:

  • 快速验证 API 接口
  • 编写自动化部署脚本
  • 模拟复杂用户行为
  • 监控服务健康状态