curl命令入门:命令行测试接口

curl命令入门:命令行测试接口

测试接口不一定要用Postman,命令行一条curl就能搞定。

今天教你curl的常用用法。

最简单的GET请求

bash 复制代码
curl https://api.example.com/users

就这么简单,返回的数据直接显示在终端。

常用参数

-X 指定请求方法:

bash 复制代码
curl -X GET https://api.example.com/users
curl -X POST https://api.example.com/users
curl -X PUT https://api.example.com/users/1
curl -X DELETE https://api.example.com/users/1

-d 发送数据:

bash 复制代码
curl -X POST https://api.example.com/login \
    -d "username=admin&password=123456"

-H 设置请求头:

bash 复制代码
curl -X POST https://api.example.com/users \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer token123"

-d 发送JSON:

bash 复制代码
curl -X POST https://api.example.com/users \
    -H "Content-Type: application/json" \
    -d '{"name":"张三","age":25}'

查看响应详情

-i 显示响应头:

bash 复制代码
curl -i https://api.example.com/users

-v 显示详细信息(调试用):

bash 复制代码
curl -v https://api.example.com/users

-o 保存到文件:

bash 复制代码
curl -o output.json https://api.example.com/users

-s 静默模式(不显示进度):

bash 复制代码
curl -s https://api.example.com/users

常用组合

POST JSON数据:

bash 复制代码
curl -X POST https://api.example.com/login \
    -H "Content-Type: application/json" \
    -d '{"username":"admin","password":"123456"}'

带Token的请求:

bash 复制代码
curl https://api.example.com/user/info \
    -H "Authorization: Bearer eyJhbGciOiJIUzI1..."

上传文件:

bash 复制代码
curl -X POST https://api.example.com/upload \
    -F "file=@/path/to/file.jpg"

下载文件:

bash 复制代码
# 保存为原文件名
curl -O https://example.com/file.zip

# 指定文件名
curl -o myfile.zip https://example.com/file.zip

处理返回结果

配合jq美化JSON:

bash 复制代码
curl -s https://api.example.com/users | jq

提取特定字段:

bash 复制代码
curl -s https://api.example.com/users | jq '.data[0].name'

只看状态码:

bash 复制代码
curl -s -o /dev/null -w "%{http_code}" https://api.example.com/users

超时设置

bash 复制代码
# 连接超时5秒
curl --connect-timeout 5 https://api.example.com

# 最大执行时间10秒
curl -m 10 https://api.example.com

实战场景

场景1:健康检查

bash 复制代码
if curl -s http://localhost:8080/health | grep -q "UP"; then
    echo "服务正常"
else
    echo "服务异常"
fi

场景2:测试接口耗时

bash 复制代码
curl -o /dev/null -s -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTotal: %{time_total}s\n" https://api.example.com

场景3:循环测试

bash 复制代码
for i in {1..10}; do
    echo "第${i}次请求"
    curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" https://api.example.com
done

速查表

用途 命令
GET请求 curl URL
POST请求 curl -X POST -d "data" URL
发送JSON curl -X POST -H "Content-Type: application/json" -d '{}' URL
带Token curl -H "Authorization: Bearer xxx" URL
显示响应头 curl -i URL
下载文件 curl -O URL
上传文件 curl -F "file=@path" URL

远程调试接口

测试服务器上的接口,直接SSH过去用curl:

bash 复制代码
ssh root@10.26.1.5
curl localhost:8080/api/users

我用星空组网把本地和服务器连起来,也可以直接从本地curl服务器接口:

bash 复制代码
curl http://10.26.1.5:8080/api/users

调试很方便,不用到处找Postman。

小结

curl最常用的几个参数:

  • -X 指定方法
  • -H 设置请求头
  • -d 发送数据
  • -i 显示响应头
  • -o 保存到文件

命令行测试接口,curl一条命令搞定。

有问题评论区交流~

相关推荐
Gauss松鼠会4 分钟前
Prometheus 实现 openGauss 的指标监控(二)Prometheus 中添加 openGauss指标
网络·数据库·oracle·prometheus·opengauss·经验总结
MXsoft6187 分钟前
## 信创运维2.0的“最后一公里”——国产设备“能用但不好用”,问题出在哪?
运维
Albert·Lin21 分钟前
LVS相关知识点总结-一
linux·服务器·lvs
一次旅行29 分钟前
act本地预跑GitHub Actions:Python项目完整CI/CD流水线实战
python·ci/cd·github
敢敢是只喵i1 小时前
Agent 为什么需要 guidance,但不能把 guidance 当成安全策略?
github·aigc
钒星物联网1 小时前
北斗二号停服倒计时!磐钴提供五大升级服务
大数据·网络
不简说1 小时前
JS 代码技巧 vol.9 — 20 个设计模式在真实项目里的应用
前端·javascript·github
古月方枘Fry1 小时前
基于大模型+MySQL的innoai助手(可适配多数环境)
网络·数据库·mysql·aigc
hz567891 小时前
应急管理部门跨部门会商用什么系统?指挥调度视频会议方案解析
网络·安全·音视频·实时音视频·信息与通信
平行宇宙喜欢吃橙1 小时前
Agent Harness 实战指南:构建生产级 AI Agent 的“马具“框架
网络·人工智能