前言
在使用idea进行开发时,如果不想在电脑上下载postman之类的测试软件,又想要进行接口测试,可以试试idea自带的http请求插件
基本使用
打开方式
单击 工具 |HTTP 客户端 |在 HTTP 客户端中创建请求。如果在编辑器中打开了请求文件,则会将请求模板添加到打开的文件中。否则,这将创建一个新的 .http 暂存文件。
也可以直接在代码的controller文件中找到要测试的接口,点击接口左边绿色按钮
最终会是这样的

如果要自己编写格式如下:
###
Method Request-URI HTTP-Version
Header-field: Header-value
Request-Body
环境切换
在.http的同级目录下,找到名字http-client.env.json
的文件,如果不存在,就创建一个.
在文件中设置全局参数
{
"local": {
"baseUrl": "http://localhost:8090"
},
"dev": {
"baseUrl": "http://dev:8090"
}
}
然后在.http文件的顶部,有环境下拉选项,在请求中使用:
POST http://localhost:9010/article/xxx
内置变量与自定义变量
内置变量:
{{$timestamp}}
时间戳,精确到秒,如需要毫秒可以在后面加三个0: 1733471556000{{$isoTimestamp}}
ISO 时间戳 输出样式: 2022-11-21T19:22:49.015031Z{{$uuid}}
uuid随机数{{$random.uuid}}
random uuid{{$random.alphabetic(5)}}
指定生成随机字符串长度, 数字 + 大小写字母{{$random.alphanumeric(5)}}
纯大小写字母随机数{{$random.email}}
随机邮件地址
自定义变量
### 测试
< {%
function padZero(num) {
return num < 10 ? '0' + num : num;
}
var date = new Date();
var year = date.getFullYear();
var month = padZero(date.getMonth() + 1); // 月份是从0开始的
var day = padZero(date.getDate());
var hours = padZero(date.getHours());
var minutes = padZero(date.getMinutes());
var seconds = padZero(date.getSeconds());
let currentDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
request.variables.set("currentDate", currentDate);
%}
POST http://localhost:8090/xxx
Content-Type: application/json
{
"createTime":{{ currentDate }}
}
其他使用记录
上传文件
### xx导入
POST http://localhost:8090/xxx/upload
Content-Type: multipart/form-data; boundary=WebKitFormBoundary
--WebKitFormBoundary
Content-Disposition: form-data; name="file"; filename="上传文件名称.xlsm";
Content-Type: multipart/form-data
< d:\file\上传文件名称.xls
--WebKitFormBoundary--
最后
IDEA的http客户端请求插件功能比较丰富,支持与postman导出的文件互转,支持WebStock请求等等,具体功能可以参考文末官方文档
参考
HTTP客户端|IntelliJ IDEA文档: https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html#composing-http-requests