news 2026/6/15 18:17:56

curl调试技巧:从HTTP请求到性能分析

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
curl调试技巧:从HTTP请求到性能分析

调试接口用Postman是挺方便,但服务器上没图形界面,只能用curl。

curl功能强大得离谱,但大部分人只会curl一个URL。这篇总结一下我常用的调试技巧。

基础请求

# GETcurlhttps://api.example.com/users# 带参数的GETcurl"https://api.example.com/users?page=1&size=10"# POST表单curl-X POST -d"username=test&password=123"https://api.example.com/login# POST JSONcurl-X POST\-H"Content-Type: application/json"\-d'{"username":"test","password":"123"}'\https://api.example.com/login# 从文件读取bodycurl-X POST -d @data.json https://api.example.com/users

查看详细信息

这才是调试的关键。

# 显示响应头curl-i https://api.example.com/users# 只显示响应头不要bodycurl-I https://api.example.com/users# 显示请求和响应的全部信息(最详细)curl-v https://api.example.com/users

-v输出很有用,能看到:

  • DNS解析
  • TCP连接
  • TLS握手
  • 发送的请求头
  • 收到的响应头
* Trying 1.2.3.4:443... * Connected to api.example.com (1.2.3.4) port 443 * TLS handshake... > GET /users HTTP/2 > Host: api.example.com > User-Agent: curl/7.68.0 > < HTTP/2 200 < content-type: application/json < ...

设置请求头

# 单个Headercurl-H"Authorization: Bearer xxx"https://api.example.com/users# 多个Headercurl-H"Authorization: Bearer xxx"\-H"X-Request-ID: abc123"\-H"Accept: application/json"\https://api.example.com/users# 覆盖默认的User-Agentcurl-A"MyApp/1.0"https://api.example.com/users

处理响应

# 保存响应到文件curl-o response.json https://api.example.com/users# 用服务器返回的文件名curl-O https://example.com/file.zip# 只要响应体,用jq格式化(需要安装jq)curl-s https://api.example.com/users|jq.# 提取某个字段curl-s https://api.example.com/users|jq'.data[0].name'

-s是静默模式,不显示进度条。

性能分析

这个很实用。排查接口慢的问题时,需要知道时间花在哪了。

curl-w"@curl-format.txt"-o /dev/null -s https://api.example.com/users

curl-format.txt内容:

time_namelookup: %{time_namelookup}s\n time_connect: %{time_connect}s\n time_appconnect: %{time_appconnect}s\n time_pretransfer: %{time_pretransfer}s\n time_redirect: %{time_redirect}s\n time_starttransfer: %{time_starttransfer}s\n ----------\n time_total: %{time_total}s\n

输出:

time_namelookup: 0.012s <- DNS解析耗时 time_connect: 0.045s <- TCP连接耗时 time_appconnect: 0.156s <- TLS握手耗时(HTTPS才有) time_pretransfer: 0.156s <- 准备传输耗时 time_redirect: 0.000s <- 重定向耗时 time_starttransfer: 0.312s <- 首字节耗时(TTFB) ---------- time_total: 0.456s <- 总耗时

最有用的是time_starttransfer,也就是TTFB(Time To First Byte),能反映服务端的处理时间。

嫌建文件麻烦,用一行命令:

curl-o /dev/null -s -w"DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n"https://api.example.com/users

模拟各种场景

带Cookie

# 发送Cookiecurl-b"session=abc123"https://api.example.com/users# 保存响应的Cookie到文件curl-c cookies.txt https://api.example.com/login# 用保存的Cookie发请求curl-b cookies.txt https://api.example.com/users

跟随重定向

# 默认不跟随,加-L跟随curl-L https://example.com/redirect

指定DNS解析

绕过DNS,直接指定IP:

curl--resolve api.example.com:443:1.2.3.4 https://api.example.com/users

测试新服务器时很有用,不用改hosts。

忽略证书错误

测试环境的自签名证书:

curl-k https://test.internal.com/api

生产环境别用-k。

限速

测试慢网络情况:

# 限制下载速度为100KB/scurl--limit-rate 100K https://example.com/file.zip

设置超时

# 连接超时5秒curl--connect-timeout5https://api.example.com/users# 整体超时10秒curl-m10https://api.example.com/users

重试

# 失败重试3次curl--retry3https://api.example.com/users# 重试间隔2秒curl--retry3--retry-delay2https://api.example.com/users

文件上传

# 上传文件curl-F"file=@/path/to/file.jpg"https://api.example.com/upload# 带其他参数curl-F"file=@/path/to/file.jpg"-F"name=test"https://api.example.com/upload# 指定文件类型curl-F"file=@/path/to/file.jpg;type=image/jpeg"https://api.example.com/upload

代理设置

# HTTP代理curl-x http://proxy.example.com:8080 https://api.example.com/users# SOCKS5代理curl--socks5127.0.0.1:1080 https://api.example.com/users

调试HTTPS

# 显示TLS握手细节curl-v --trace-ascii - https://api.example.com/users# 指定TLS版本curl--tlsv1.2 https://api.example.com/users# 查看证书信息curl-vI https://api.example.com2>&1|grep-A6"Server certificate"

实用场景

测试接口是否正常

# 只关心状态码curl-s -o /dev/null -w"%{http_code}"https://api.example.com/health

批量测试

# 测试10次,看平均响应时间foriin{1..10};docurl-s -o /dev/null -w"%{time_total}\n"https://api.example.com/usersdone

对比两个环境

# 测试环境curl-s https://test.example.com/api/users|md5sum# 生产环境curl-s https://api.example.com/users|md5sum

响应一样就md5一样。

生成代码

curl可以直接生成各种语言的代码:

# 转成Pythoncurlhttps://api.example.com/users --libcurl output.py# 或者用在线工具把curl命令转成代码

常用alias

加到/.bashrc或/.zshrc:

# 格式化JSON响应aliascurljson='curl -s | jq .'# 只看状态码aliascurlcode='curl -s -o /dev/null -w "%{http_code}\n"'# 带详细信息aliascurlv='curl -v'# 带性能信息aliascurltime='curl -o /dev/null -s -w "DNS: %{time_namelookup}s | Connect: %{time_connect}s | TTFB: %{time_starttransfer}s | Total: %{time_total}s\n"'

用起来:

curljson https://api.example.com/users curlcode https://api.example.com/health curltime https://api.example.com/users

curl的参数太多,没必要都记住。知道有这些功能,用的时候来查就行。

跨服务器调试时,如果网络不通,我一般用星空组网先把几台机器串起来,然后curl各种测。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/15 6:48:56

广告创意自动生成平台:一键产出多版本素材

广告创意自动生成平台&#xff1a;一键产出多版本素材 在广告投放节奏以“小时”甚至“分钟”为单位快速迭代的今天&#xff0c;品牌方早已无法依赖传统设计团队手动制作海报、视频和文案。一个双十一大促活动可能需要数百套视觉素材覆盖不同人群、渠道和情绪风格&#xff0c;而…

作者头像 李华
网站建设 2026/6/13 18:47:59

Java并发编程中的final域介绍

与锁和volatile相比,对final域的读和写更像是普通的变量访问。下面将介绍final域的内存语义。 一、final域的重排序规则 对于final域,编译器和处理器要遵守两个重排序规则。 1)在构造函数内对一个final域的写入,与随后把这个被构造对象的引用赋值给一个引用变量,这两个…

作者头像 李华
网站建设 2026/6/15 12:01:42

java计算机毕业设计校园旧物交易系统 高校二手闲置物品交易平台的设计与实现 基于SpringBoot的校园跳蚤市场系统

计算机毕业设计校园旧物交易系统m198z9&#xff08;配套有源码 程序 mysql数据库 论文&#xff09; 本套源码可以在文本联xi,先看具体系统功能演示视频领取&#xff0c;可分享源码参考。毕业季“搬家季”&#xff0c;成堆的教材、台灯、小风扇从五楼搬到垃圾站&#xff0c;不如…

作者头像 李华
网站建设 2026/6/15 13:17:50

Springboot家政服务管理系统w6nqa(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。

系统程序文件列表项目功能&#xff1a;用户,员工,服务项目,服务类型,服务订单,服务评价开题报告内容一、选题背景与意义1.1 选题背景随着城市化进程加速和生活节奏加快&#xff0c;家政服务需求呈现爆发式增长。据国家统计局数据显示&#xff0c;2024年我国家政服务业市场规模突…

作者头像 李华
网站建设 2026/6/15 12:01:27

含储能及sop的多时段配网优化模型Matlab实现

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。 &#x1f34e; 往期回顾关注个人主页&#xff1a;Matlab科研工作室 &#x1f34a;个人信条&#xff1a;格物致知,完整Matlab代码获取及仿…

作者头像 李华