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

有问题评论区交流~

相关推荐
修己xj9 小时前
差生文具多?我给自己改造了一款AI周计划工具
github
不像程序员的程序媛10 小时前
系统cpu内存负载资源分析
运维·服务器·数据库
Uncertainty!!11 小时前
Ubuntu 22.04 安装超好用中文输入法rime-ice(雾凇拼音)过程记录
linux·ubuntu·中文输入法
kk的vmware虚拟机安装ubuntu12 小时前
鱼皮 yu-rpc:从 0 到 1 手写 RPC 框架的实践教程
网络·网络协议·其他·rpc
ShineWinsu12 小时前
对于Linux:网络基础的解析
linux·网络·面试·udp·笔试·ip·tcp
Asuicao12 小时前
centos7.9版本升级ssh OpenSSH 9.8p1 源码 OpenSSL 1.1.1w 源码包以及安装升级回滚文档
运维·ssh
耍酷的魔镜13 小时前
谈谈如何使用Netty开发实现高性能的RPC服务器
服务器·网络协议·rpc
阿里嘎多学长13 小时前
2026-07-07 GitHub 热点项目精选
开发语言·程序员·github·代码托管
志栋智能13 小时前
超自动化安全中的威胁狩猎
运维·安全·自动化
↘"LYong13 小时前
旧版 CentOS 安装 Docker 完整指南(附国内镜像加速)
linux·运维·docker