news 2026/4/1 17:24:51

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/3/31 21:06:15

在线教育防刷课机制:学习过程真实性验证

在线教育防刷课机制&#xff1a;学习过程真实性验证 在远程教学日益普及的今天&#xff0c;一个看似平静的学习界面背后&#xff0c;可能正上演着一场“人机对抗”——学生用自动化脚本挂机、多开虚拟机刷课、循环播放录屏视频&#xff0c;只为快速拿到学分。而平台方则不断升级…

作者头像 李华
网站建设 2026/3/25 11:50:03

电商运营数据分析的系统架构可适应性

运营数据分析的系统架构可适应性 关键词:运营数据分析、系统架构、可适应性、数据处理、业务变化 摘要:本文围绕运营数据分析的系统架构可适应性展开深入探讨。首先介绍了研究的背景、目的、预期读者和文档结构等内容。接着阐述了核心概念及其联系,通过文本示意图和 Mermaid…

作者头像 李华
网站建设 2026/3/26 16:09:09

新品上市效果预测:市场营销前期评估工具

新品上市效果预测&#xff1a;基于 TensorRT 的高性能推理实践 在消费品企业推出一款新品之前&#xff0c;市场团队最常问的问题是&#xff1a;“这款产品能卖多少&#xff1f;” 过去&#xff0c;这个问题的答案往往依赖于经验判断、小范围试销或简单的回归模型。但今天&#…

作者头像 李华
网站建设 2026/3/30 13:53:03

盲文输出转换工具:视障用户的信息入口

盲文输出转换工具&#xff1a;视障用户的信息入口 在数字信息爆炸的时代&#xff0c;屏幕上的每一个字符、每一张图片都可能成为视障群体难以逾越的“视觉高墙”。尽管语音读屏技术已广泛应用&#xff0c;但在需要精准阅读、反复确认或私密浏览的场景下&#xff0c;盲文依然是不…

作者头像 李华
网站建设 2026/4/1 0:30:21

系统崩溃根因定位:AI辅助故障诊断实践

系统崩溃根因定位&#xff1a;AI辅助故障诊断实践 在一次深夜的线上事故中&#xff0c;某大型云服务平台突然出现大规模服务降级。监控系统显示多个微服务响应延迟飙升&#xff0c;但日志中并未记录明显错误信息。运维团队紧急排查网络、数据库和中间件后仍无法锁定问题源头—…

作者头像 李华