当前位置:网站首页>IDEA rest-client,会了它我还没打开过postman
IDEA rest-client,会了它我还没打开过postman
2020-11-09 12:28:00 【Ze_Sui】
1. 简介
IDEA本身是提供测试接口工具的, rest-client/http client,功能很强大,在我日常使用中,完全可以做到不需要打开 postman 的程度。 入口如下:

<center>图一 打开方式</center>
点击 Test RESTful Web Service 是打开 Rest Client 工具,可以理解为一个使用了IDEAUI的接口测试工具,相对比较可视化,直接在上面根据实际需求填充值即可,再点左上角的按钮启动测试。

<center>图二 rest client</center>
图一选项中的第二个选项 Open Http Requests Collection 才是 今天的重点,可以直接通过 .http 后缀的文件来请求接口。除了在工具栏进去,也可以在任何地方自己 new 一个出来(图4),或者点击图二中黄色底的 Convert request to the new format ,也会在 Scratches and consoles 下的 Scratches文件夹 生成一个 .http文件(图五)

<center>图三 http client</center>

<center> 图四</center>

<center>图五 </center>
2. http client
下面主要讲一下 http client 的使用,简单理解就是 http请求的参数,都可以在这里自行定义。
刚刚进入文件会给你一个例子,第一行很容易理解,选择请求方式(GET、POST、PUT等) 以及 请求地址,这是必填项;第二行开始并不一定是要有的,不给的话就根据你的请求方式按照默认参数来给
GET http://localhost:8080/resources
Accept: */*
Cache-Control: no-cache
2.1 普通请求
请求成功之后会在在下方控制台显示请求的内容,以及结果,并在 .http 文件的对应请求下方留有一条历史记录,方便查看(如图六)

<center>图六 http client运行效果</center>
2.2 变量请求
系统经常会部署在不同的环境之中,http client 同时也支持根据不同的环境修改参数,支持的参数有:
- 请求ip,端口和路径
- 查询参数或值
- 请求头的值
- 在外部文件为请求体提供任意值
支持的环境变量定义文件有具体的命名方式,并且需要跟 .http文件在同一目录下(Scratches and consoles文件夹下面的只能普通请求),命名也有要求,需要是下面的其中之一:
- rest-client.env.json
- http-client.env.json
- rest-client.private.env.json
- http-client.private.en.json
上述文件中带private的文件不表示是一个私人的文件,可能包括密码,令牌,证书等敏感信息,默认情况下,此文件被添加到VCS忽略文件列表中。使用如下(内容是 Json 格式):

<center>图七 环境变量文件设置</center>

<center>图八 运行时选择</center>
如图七 图八,在设置完环境变量之后运行时就可以选择相关的环境变量传入请求中,直接运行即可
3. 使用小技巧
3.1 语法
- 注释使用
//或者# - 分开请求用
### - 如果不想要生成日志
- 脚本 以
>开头,将逻辑写在{% %}中,可以调用client对象

<center>图九 不记录日志</center>
3.2 快捷键
| 快捷键 | 说明 |
|---|---|
| gtr | 生成一个get请求 |
| gtrp | 生成一个get请求,外加参数 |
| ptr | 生成一个post请求,格式是application/json |
| ptrt | 生成一个post请求,格式是application/x-www-form-urlencoded |
| mptr | 生成一个post请求,表单提交 |
| fptr | 生成一个post请求,文件上传 |
###
# gtr
GET http://localhost:80/api/item
Accept: application/json
###
# gtrp
GET http://localhost:80/api/item?id=99
Accept: application/json
###
# prt
POST http://localhost:80/api/item
Content-Type: application/json
{}
###
#ptrp
POST http://localhost:80/api/item
Content-Type: application/x-www-form-urlencoded
id=99&content=new-element
###
# mptr
POST http://localhost:80/api/item
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="field-name"
field-value
--WebAppBoundary--
###
# fprt
POST http://localhost:80/api/item
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="field-name" filename="file.txt"
< ./relative/path/to/local_file.txt
--WebAppBoundary--
###
3.3 脚本
可以在请求中添加脚本,打印或者存储一些信息,可以使用脚本存储一些结果变量,然后类似环境变量一样的引用。

<center>图十 脚本示例</center>
如果使用了脚本的打印,结果控制台会出现一个选项框,显示打印内容,如下方的 Respinse Handler

3.4 多看示例
IDEA在 .http 文件的右边已经给我们提供了很多例子,以及便捷操作,当自己无从下手的时候,也可以参考一下。还提供了从 cURL 转换为 .http 文件可识别的请求功能。

本文由博客一文多发平台 OpenWrite 发布!
版权声明
本文为[Ze_Sui]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4186000/blog/4708443
边栏推荐
- Wealth and freedom? Ant financial services suspended listing, valuation or decline after regulation
- Interface tests how to pass files in post requests
- FGC online service troubleshooting, this is enough!
- SQL statement to achieve the number of daffodils
- Is SEO right or wrong?
- Download Netease cloud music 10W + music library with Python
- In the future, China Telecom will make cloud computing service the main business of China Telecom
- Adobe Experience Design /Xd 2020软件安装包(附安装教程)
- Online course of tutorial system processing is in progress
- TiDB x 微众银行 | 耗时降低 58%,分布式架构助力实现普惠金融
猜你喜欢

Front end code style practice prettier + eslint + git hook + lint staged

Vscode plug-in configuration pointing North

SEO见风使舵,是对还是错?

SQL Chapter 2 Chapter 3

Configure switch trunk interface traffic local priority forwarding (cluster / stack)

通配符SSL证书应该去哪里注册申请

10款必装软件,让Windows使用效率飞起!

为wget命令设置代理

在嵌入式设备中实现webrtc的第三种方式③

Stack & queue (go) of data structure and algorithm series
随机推荐
Safety (miscellany)
Android rights
20201107第16课,使用Apache服务部署静态网站;使用Vsftpd服务传输文件
微信圈子
Well, these four ways to query the maximum value of sliding window are good
Adobe experience design / XD 2020 software installation package (with installation tutorial)
Vscode plug-in configuration pointing North
配置交换机Trunk接口流量本地优先转发(集群/堆叠)
As a user, you can't get rid of the portrait!
Is SEO right or wrong?
Well, the four ways to query the maximum value of sliding window are good
Use treeview tree menu bar (recursively call database to create menu automatically)
EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL
走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
一个简单的能力,决定你是否会学习!
Interface tests how to pass files in post requests
JVM学习(五) -执行子系统
Aren't you curious about how the CPU performs tasks?
如何保证消息不被重复消费?(如何保证消息消费的幂等性)
使用TreeView树型菜单栏(递归调用数据库自动创建菜单)