news 2026/5/10 4:45: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/5/4 22:37:27

容量规划预测模型:基础设施投入精准测算

容量规划预测模型&#xff1a;基础设施投入精准测算 在AI服务大规模上线的今天&#xff0c;一个看似简单的问题却困扰着无数工程团队&#xff1a;我们到底需要多少GPU&#xff1f;采购少了&#xff0c;大促期间系统崩盘&#xff1b;买多了&#xff0c;资源常年闲置&#xff0c;…

作者头像 李华
网站建设 2026/5/5 2:58:03

日志留存策略优化:存储成本与法规遵从平衡

TensorRT 推理优化实战&#xff1a;如何释放 GPU 的极致性能 在自动驾驶系统每秒处理上千帧图像、智能客服要求毫秒级响应的今天&#xff0c;模型推理早已不再是“能跑就行”的阶段。当一个训练好的 PyTorch 模型从实验室走向生产环境时&#xff0c;真正的挑战才刚刚开始——我…

作者头像 李华
网站建设 2026/4/27 1:37:24

NVIDIA TensorRT镜像安装与配置最简教程

NVIDIA TensorRT镜像安装与配置最简教程 在AI模型日益复杂、部署场景愈加多样化的今天&#xff0c;推理性能已经成为决定系统能否落地的关键瓶颈。一个训练得再好的模型&#xff0c;如果在线上服务中响应迟缓、吞吐低下&#xff0c;那它的实际价值将大打折扣。尤其是在自动驾驶…

作者头像 李华
网站建设 2026/5/3 15:46:42

多语言翻译服务质量保障:通信无国界的基石

多语言翻译服务质量保障&#xff1a;通信无国界的基石 在全球化浪潮席卷各行各业的今天&#xff0c;企业跨国协作、科研机构联合攻关、用户跨语言社交已成常态。然而&#xff0c;语言鸿沟依然是信息流通的隐形壁垒。尽管深度学习驱动的神经机器翻译&#xff08;NMT&#xff09;…

作者头像 李华
网站建设 2026/4/29 1:23:56

跨区域数据同步加速:全球化业务的底层支撑

跨区域数据同步加速&#xff1a;全球化业务的底层支撑 在当今全球化的数字生态中&#xff0c;用户对服务响应速度的容忍度正变得越来越低。无论是欧洲消费者在午夜下单购物、东南亚用户与语音助手对话&#xff0c;还是美洲金融机构进行实时反欺诈决策&#xff0c;他们都不希望因…

作者头像 李华
网站建设 2026/4/22 12:14:24

植物养护提醒机器人:阳台绿植不再轻易枯萎

植物养护提醒机器人&#xff1a;阳台绿植不再轻易枯萎 在城市生活的方寸阳台上&#xff0c;一盆绿植往往承载着人们对自然的向往。然而&#xff0c;工作繁忙、出差频繁&#xff0c;常常让人忘记浇水、忽视光照——再顽强的生命也扛不住长期疏于照料。于是&#xff0c;我们开始思…

作者头像 李华