news 2026/5/9 1:41:26

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/5/2 10:04:29

实测对比:原生PyTorch vs TensorRT推理性能差距惊人

实测对比&#xff1a;原生PyTorch vs TensorRT推理性能差距惊人 在AI模型从实验室走向真实世界的最后一公里&#xff0c;性能的微小提升往往意味着成本的大幅下降。你有没有遇到过这样的场景&#xff1f;训练好的模型部署上线后&#xff0c;明明参数量不算大&#xff0c;却在实…

作者头像 李华
网站建设 2026/5/1 15:28:44

RK3576-Android15原生相机Camera2 修改USB相机预览和成像方向

提示&#xff1a;RK3576-Android15 谷歌原生相机Camera2 使用客户定制的USB相机&#xff0c;发现预览和成像方向错位&#xff0c;相机软件需要适配USB相机 文章目录前言-需求一、基础-参考资料-思路参考资料基础补充架构图了解Camera相关专栏零散知识了解部分相机源码参考&…

作者头像 李华
网站建设 2026/5/3 17:00:22

ODA X9-2 双归档路径配置

周五下午客户突然反馈有套ODA X9-2的orcl实例报归档满了&#xff0c;目前没运维&#xff0c;需要帮忙处理一下问题&#xff0c;登录环境后发现db_recovery_file_dest_size配置了300G&#xff0c;当前使用率100%了救急情况 先把db_recovery_file_dest_size 扩展到400G&#xff0…

作者头像 李华
网站建设 2026/5/2 15:52:48

AI agents协作分析卫星图像:评估公司实际经营状况

AI agents协作分析卫星图像:评估公司实际经营状况 关键词:AI agents、卫星图像分析、公司经营状况评估、多智能体协作、遥感技术 摘要:本文聚焦于利用AI agents协作分析卫星图像以评估公司实际经营状况这一前沿技术。首先介绍了该技术的背景,包括目的、预期读者等。接着阐述…

作者头像 李华
网站建设 2026/5/1 16:09:06

新闻稿件自动生成上线:媒体行业的生产力变革

新闻稿件自动生成上线&#xff1a;媒体行业的生产力变革 在信息爆炸的时代&#xff0c;一条突发新闻从发生到传播的“黄金窗口”可能只有几分钟。当某地发生地震、股市异动或重大政策发布时&#xff0c;谁能在最短时间内产出准确、清晰的报道&#xff0c;谁就掌握了话语权。传…

作者头像 李华
网站建设 2026/5/1 6:18:55

AI绘画提示词优化器上线:创意工作者的新工具

AI绘画提示词优化器上线&#xff1a;创意工作者的新工具 在设计师等待AI生成一张图像的几秒钟里&#xff0c;灵感可能已经溜走。如今&#xff0c;越来越多的内容创作者发现&#xff0c;他们不是在“使用”AI绘画工具&#xff0c;而是在“忍受”它——输入提示词、点击生成、盯着…

作者头像 李华