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一条命令搞定。

有问题评论区交流~

相关推荐
wdfk_prog3 分钟前
[Linux]学习笔记系列 -- [drivers][I2C]I2C
linux·笔记·学习
猫头虎12 分钟前
如何解决 OpenClaw “Pairing required” 报错:两种官方解决方案详解
网络·windows·网络协议·macos·智能路由器·pip·scipy
VekiSon13 分钟前
Linux内核驱动——杂项设备驱动与内核模块编译
linux·c语言·arm开发·嵌入式硬件
tianyuanwo15 分钟前
企业级NTP客户端配置指南:基于内部NTP服务器的实践
运维·服务器·ntp客户端
芷栀夏20 分钟前
CANN开源实战:基于DrissionPage构建企业级网页自动化与数据采集系统
运维·人工智能·开源·自动化·cann
Y1rong25 分钟前
linux之网络
linux
寄存器漫游者43 分钟前
Linux 软件编程 - IO 编程
linux·运维·spring
charlotte102410241 小时前
高并发:关于在等待学校教务系统选课时的碎碎念
java·运维·网络
_别来无恙_1 小时前
TFTP的使用Linux
linux·服务器
gaize12131 小时前
Moltbot(Clawdbot) 专属轻量服务器
运维·服务器