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

有问题评论区交流~

相关推荐
wj3055853785 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李5 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
abigriver5 小时前
打造 Linux 离线大模型级语音输入法:Whisper.cpp + 3090 显卡加速与 Rime 中英混输终极调优指南
linux·运维·whisper
wangqiaowq6 小时前
windows下nginx的安装
linux·服务器·前端
YYRAN_ZZU6 小时前
Petalinux新建自动脚本启动
linux
charlie1145141917 小时前
嵌入式Linux驱动开发pinctrl篇(1)——从寄存器到子系统:驱动演进之路
linux·运维·驱动开发
Agent手记7 小时前
异常考勤智能预警与处理与流程优化方案 | 基于企业级Agent的超自动化实战教程
运维·人工智能·ai·自动化
于小猿Sup7 小时前
VMware在Ubuntu22.04驱动Livox Mid360s
linux·c++·嵌入式硬件·自动驾驶
cen__y7 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
海蓝可知天湛9 小时前
Agent&IELTS雅思口语专属语料库
人工智能·github·rag·ielts·skills