curl 命令详解(2026):GET/POST 请求、文件传输与 API 调试实战

curl 命令完整教程:GET/POST/PUT/DELETE 请求、JSON 数据发送、文件上传下载、鉴权认证、代理配置与耗时分析,一文速查全部用法。

Bruce

curlLinuxHTTPAPI运维命令行

Linux

929 Words

2020-06-29 00:00 +0000


在日常开发和运维中,curl 命令几乎是使用频率最高的命令行工具之一。它支持 HTTP、HTTPS、FTP 等 20+ 种协议,无论是调试 REST API、发送 POST 请求、下载文件还是测试网络连通性,一条 curl 命令就能搞定。

本文是一份系统的 curl 命令详解教程,涵盖 GET/POST/PUT/DELETE 等 HTTP 请求方法、JSON 数据发送、文件上传下载、Bearer Token 认证、代理配置、耗时分析等全部核心用法。如果你还需要排查网络链路问题,可以配合 traceroute 命令详解 一起使用。

为什么要学 curl?

场景curl 的优势
API 调试快速发送各种 HTTP 请求,无需安装额外工具
自动化脚本轻松集成到 Shell 脚本中
网络诊断查看详细的请求/响应信息
文件传输支持断点续传、限速下载
跨平台Linux、macOS、Windows 通用

掌握 curl,你就掌握了一个万能的网络瑞士军刀。

一、基础用法

1. 发送 GET 请求

最简单的用法,直接跟 URL:

# 获取网页内容
curl https://httpbin.org/get

# 将响应保存到文件
curl -o response.json https://httpbin.org/get

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

2. 显示详细信息

调试时最有用的选项:

# -v 显示详细的请求/响应过程
curl -v https://httpbin.org/get

# -i 显示响应头
curl -i https://httpbin.org/get

# -I 只获取响应头(HEAD 请求)
curl -I https://httpbin.org/get

# 静默模式,只显示结果
curl -s https://httpbin.org/get

-v 输出解读

  • > 开头:发送的请求
  • < 开头:收到的响应
  • * 开头:curl 的处理信息

3. 跟随重定向

很多 URL 会返回 301/302 重定向,使用 -L 自动跟随:

# 自动跟随重定向
curl -L https://github.com

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

二、HTTP 请求方法

1. POST 请求

# 发送表单数据(application/x-www-form-urlencoded)
curl -X POST -d "name=john&[email protected]" https://httpbin.org/post

# 发送 JSON 数据
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"john","email":"[email protected]"}' \
  https://httpbin.org/post

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

2. PUT 请求

# 更新资源
curl -X PUT \
  -H "Content-Type: application/json" \
  -d '{"id":1,"name":"john","status":"active"}' \
  https://httpbin.org/put

3. DELETE 请求

# 删除资源
curl -X DELETE https://httpbin.org/delete

# 带认证的删除
curl -X DELETE \
  -H "Authorization: Bearer your_token" \
  https://api.example.com/users/123

4. PATCH 请求

# 部分更新
curl -X PATCH \
  -H "Content-Type: application/json" \
  -d '{"status":"inactive"}' \
  https://httpbin.org/patch

三、请求头与认证

1. 自定义请求头

# 添加单个请求头
curl -H "Authorization: Bearer token123" https://api.example.com

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

# 设置 User-Agent
curl -A "Mozilla/5.0 (Macintosh)" https://example.com

# 设置 Referer
curl -e "https://google.com" https://example.com

2. 基本认证

# 用户名密码认证
curl -u username:password https://api.example.com

# 交互式输入密码(更安全)
curl -u username https://api.example.com

3. Bearer Token 认证

curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
  https://api.example.com/users
# 发送 Cookie
curl -b "session_id=abc123" https://example.com

# 从文件读取 Cookie
curl -b cookies.txt https://example.com

# 保存响应的 Cookie
curl -c cookies.txt https://example.com

# 读取并保存 Cookie(模拟浏览器会话)
curl -b cookies.txt -c cookies.txt https://example.com

四、文件上传与下载

1. 文件下载

# 下载文件并重命名
curl -o myfile.zip https://example.com/file.zip

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

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

# 限速下载(100KB/s)
curl --limit-rate 100k -O https://example.com/file.zip

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

2. 文件上传

# 上传单个文件(multipart/form-data)
curl -F "file=@/path/to/file.jpg" https://httpbin.org/post

# 上传多个文件
curl -F "[email protected]" -F "[email protected]" https://httpbin.org/post

# 上传文件并指定 MIME 类型
curl -F "[email protected];type=image/jpeg" https://httpbin.org/post

# 上传文件同时带其他表单字段
curl -F "[email protected]" -F "description=My Document" https://httpbin.org/post

五、HTTPS 与证书

1. 忽略证书验证

# 跳过 SSL 证书验证(测试环境使用)
curl -k https://self-signed.example.com

# 等同于
curl --insecure https://self-signed.example.com

2. 指定证书

# 指定 CA 证书
curl --cacert /path/to/ca.crt https://example.com

# 使用客户端证书
curl --cert /path/to/client.crt --key /path/to/client.key https://example.com

3. 指定 TLS 版本

# 强制使用 TLS 1.2
curl --tlsv1.2 https://example.com

# 强制使用 TLS 1.3
curl --tlsv1.3 https://example.com

六、代理设置

1. HTTP 代理

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

# 带认证的代理
curl -x http://user:[email protected]:8080 https://target.com

# 通过环境变量设置(永久生效)
export http_proxy=http://proxy.example.com:8080
export https_proxy=http://proxy.example.com:8080
curl https://target.com

2. SOCKS 代理

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

# SOCKS5 代理(DNS 也通过代理解析)
curl --socks5-hostname 127.0.0.1:1080 https://example.com

七、超时与重试

# 连接超时(秒)
curl --connect-timeout 10 https://example.com

# 总超时时间(秒)
curl -m 30 https://example.com
curl --max-time 30 https://example.com

# 自动重试
curl --retry 3 https://example.com

# 重试间隔
curl --retry 3 --retry-delay 5 https://example.com

八、指定 IP 访问

调试 CDN 或负载均衡时非常有用:

# HTTP:通过代理方式指定 IP
curl -x 192.168.1.100:80 "http://example.com/api"

# HTTPS:通过 Host 头指定域名
curl -H "Host: example.com" "https://192.168.1.100/api" -k

# 使用 --resolve 解析域名到指定 IP(推荐)
curl --resolve example.com:443:192.168.1.100 https://example.com/api

# 将输出丢弃,只看请求过程
curl -vo /dev/null --resolve example.com:443:192.168.1.100 https://example.com

九、实用技巧

1. 格式化 JSON 输出

# 配合 jq 格式化
curl -s https://httpbin.org/get | jq .

# 只提取特定字段
curl -s https://httpbin.org/get | jq '.headers'

2. 测量请求时间

# 显示各阶段耗时
curl -w "\n--- 耗时统计 ---\n\
DNS解析: %{time_namelookup}s\n\
TCP连接: %{time_connect}s\n\
SSL握手: %{time_appconnect}s\n\
首字节: %{time_starttransfer}s\n\
总耗时: %{time_total}s\n" \
  -o /dev/null -s https://example.com

3. 模拟浏览器请求

curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36" \
     -H "Accept: text/html,application/xhtml+xml" \
     -H "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8" \
     -e "https://google.com" \
     https://example.com

4. 发送压缩请求

# 请求压缩响应
curl --compressed https://example.com

十、常用选项速查表

选项说明示例
-X指定请求方法-X POST
-d发送 POST 数据-d '{"key":"value"}'
-H添加请求头-H "Content-Type: application/json"
-o输出到文件-o file.txt
-O使用远程文件名保存-O
-L跟随重定向-L
-v显示详细信息-v
-s静默模式-s
-i显示响应头-i
-I只获取响应头-I
-k忽略 SSL 证书-k
-u基本认证-u user:pass
-b发送 Cookie-b "name=value"
-c保存 Cookie-c cookies.txt
-F上传文件-F "file=@path"
-x使用代理-x proxy:port
-m超时时间-m 30
-AUser-Agent-A "Mozilla/5.0"
-eReferer-e "https://ref.com"

十一、curl 与 wget 对比

对比项curlwget
协议支持20+ 种(HTTP、FTP、SMTP 等)HTTP、HTTPS、FTP
输出方式默认输出到 stdout默认保存到文件
递归下载不支持支持(-r
HTTP 方法全部支持(GET/POST/PUT/DELETE 等)仅 GET/POST
上传功能支持不支持
管道处理非常方便(配合 jq 等)不便
适用场景API 调试、脚本集成、数据传输批量下载、网站镜像

简单来说:调试 API 用 curl,批量下载用 wget

十二、常见问题

Q1:curl 返回乱码怎么办?

可能是服务端返回了压缩数据,使用 --compressed 参数自动解压:

curl --compressed https://example.com

Q2:如何用 curl 发送 JSON 请求?

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"test","value":123}' \
  https://httpbin.org/post

关键是添加 Content-Type: application/json 请求头,并用 -d 传递 JSON 字符串。

Q3:curl 如何忽略 SSL 证书错误?

使用 -k--insecure 参数(仅限测试环境):

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

Q4:如何查看 curl 请求的完整过程?

使用 -v(verbose)参数查看详细的请求和响应信息:

curl -v https://example.com

Q5:curl 如何设置超时时间?

使用 --connect-timeout 设置连接超时,-m(或 --max-time)设置总超时:

# 连接超时 5 秒,总超时 30 秒
curl --connect-timeout 5 -m 30 https://example.com

Q6:curl 和 Postman 有什么区别?

curl 是命令行工具,轻量、可脚本化、适合自动化和 CI/CD 流水线;Postman 是 GUI 工具,可视化操作更友好,适合团队协作和复杂的 API 测试集合管理。两者可互补,Postman 还支持将请求导出为 curl 命令。

Q7:如何用 curl 测量网站响应速度?

使用 -w 参数输出各阶段耗时:

curl -w "DNS: %{time_namelookup}s\nTCP: %{time_connect}s\n首字节: %{time_starttransfer}s\n总耗时: %{time_total}s\n" -o /dev/null -s https://example.com

总结

curl 是一个功能强大的命令行工具,掌握它能大幅提升你的工作效率。本文介绍了最常用的场景:

  1. 基础请求:GET/POST/PUT/DELETE
  2. 认证方式:Basic、Bearer Token、Cookie
  3. 文件传输:上传、下载、断点续传
  4. HTTPS 处理:证书验证、TLS 版本
  5. 调试技巧:指定 IP、测量耗时、格式化输出

建议把常用命令保存为 Shell 函数或别名,更多 curl 高级用法可参考官方文档

相关阅读

参考资源

Comments

Join the discussion — requires a GitHub account