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

有问题评论区交流~

相关推荐
钱栈up13 分钟前
GitHub 热榜上手|anthropics/skills:18.9万星的 Claude 技能库
python·开源·github
李永奉7 小时前
Git和GitHub-VS Code 图形化 Git 放弃更改、暂存与取消暂存、撤销上一次提交(VS Code 界面操作 + 命令行对照)
git·github
郝学胜-神的一滴9 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
Johny_Zhao9 小时前
Keepalived与Nginx架构选型
linux·nginx·信息安全·云计算·apache·shell·yum源·keepalive·系统运维
TunerT_TQ11 小时前
【开源 L3 静态取证|工程评测】xnulinux:双内核实验——可复现的环境与许可比截图更重要
github
一条Go11 小时前
proto 契约:校验注解、错误码与「文案即接口」
后端·github
一条Go11 小时前
15 套品牌色 + 双模式:把颜色关进三层 token
前端·github
FlightYe11 小时前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
by2099912 小时前
Linux 文件描述符与重定向
linux·服务器