IDEA 接口调试插件 HTTP Client

码途织梦者
• 阅读 3024

界面客户端

使用手册
https://www.jetbrains.com/hel...
<br/><br/>
打开方式<br/>
Tools -> HTTP Client -> Test RESTful Web Service

IDEA 接口调试插件 HTTP Client

<br/><br/>

文本客户端

<br/>
使用手册
https://www.jetbrains.com/hel...

特点

  • 纯文本编写
  • 支持统一配置
  • 支持 scripts 脚本

<br/><br/>
创建新的请求文件

  • Scratch files (全局文件)
  • physical files(项目文件)

IDEA 接口调试插件 HTTP Client

<br/><br/>
live templates

IDEA 接口调试插件 HTTP Client

<br/><br/><br/><br/>

支持 HTTP 1.1 所有方法

<br/>
POST、GET、PUT、DELETE、HEAD、OPTIONS、TRACE、CONNECT
<br/><br/>

GET

### Get request with a header
GET https://httpbin.org/ip
Accept: application/json

### Get request with parameter
GET https://httpbin.org/get?show_env=1
Accept: application/json

### Get request with environment variables
GET {{host}}/get?show_env={{show_env}}
Accept: application/json

### Get request with disabled redirects
# @no-redirect
GET http://httpbin.org/status/301

<br/><br/>

POST

### Send POST request with json body
POST https://httpbin.org/post
Content-Type: application/json

{
  "id": 999,
  "value": "content"
}

### Send POST request with body as parameters
POST https://httpbin.org/post
Content-Type: application/x-www-form-urlencoded

id=999&value=content

### Send a form with the text and file fields
POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="element-name"
Content-Type: text/plain

Name
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data.json"
Content-Type: application/json

< ./request-form-data.json
--WebAppBoundary--

###

<br/><br/>

Post请求的两种编码格式

<br/>

  • application/x-www-form-urlencoded

post的默认格式,使用js中URLencode转码方法。

包括将name、value中的空格替换为加号;将非ascii字符做百分号编码;将input的name、value用‘=’连接,不同的input之间用‘&’连接。

跟get的区别在于,get把转换、拼接完的字符串用‘?’直接与表单的action连接作为URL使用,所以请求体里没有数据

  • multipart/form-data

将表单中的每个input转为了一个由boundary分割的小格式,没有转码,直接将utf8字节拼接到请求体中,在本地有多少字节实际就发送多少字节,极大提高了效率,适合传输长字节

<br/><br/><br/>

查看请求历史

点击右上角的按钮
Show HTTP Requests History

IDEA 接口调试插件 HTTP Client

或者在工具栏内打开Tools | HTTP Client | Show HTTP Requests History

IDEA 接口调试插件 HTTP Client
<br/><br/><br/><br/>

演示接口重构 - 统一配置

<br/><br/>

定义环境变量

环境变量需要定义在环境文件中,环境文件有两种:

  1. 创建名为 rest-client.env.json 或者 http-client.env.json 的环境文件(其实里面就是保存 JSON 数据),该文件里可以定义用在整个项目上的所有常规变量
  2. 创建名为rest-client.private.env.json 或者 http-client.private.env.json, 看文件名你应该也猜到这是保存敏感数据的,比如密码,token等,该文件默认是被加入到 VCS 的 ignore文件中的,同时优先级高于其他环境文件, 也就是说,该文件的变量会覆盖其他环境文件中的变量值
{
  "dev": {
    "host": "localhost",
    "port": 8081,
    "identifier": "tanrgyb",
    "password": "iloveu"
  },
  "prod": {
    "host": "dayarch.top",
    "port": 8080,
    "identifier": "admin",
    "password": "admin"
  }
}

<br/><br/>

通用配置,域名/端口

rest-client.env.json 或 http-client.env.json
{
  "default": {

  },
  "local": {
    "host": "http://localhost:8080"
  }
}

<br/><br/>

个人安全配置,用户名/密码

rest-client.private.env.json 或 http-client.private.env.json
{
  "default": {

  },
  "local": {
    "account": "admin",
    "password": "123456"
  }
}

<br/><br/>

重构后的请求文件

### 登陆
POST {{host}}/api/login
Content-Type: application/x-www-form-urlencoded

account=lee&password=123456

### 测试接口
GET {{host}}/api/security/test
Accept: application/json
Cookie: JSESSIONID=1C1DD3EB60DEE60664FB0BFE0F1C9942

###

<br/><br/>
运行请求,点击运行按钮,可以选择对应的环境

使用 response handler scripts

<br/><br/>

引用方式

<br/>

  • 直接引用
GET host/api/test

> {%
// Response Handler Script
...
%}

<br/><br/>

  • 文件引用
GET host/api/test

> scripts/my-script.js

<br/><br/><br/>

主要方法

<br/><br/>
HTTP Response handling API reference

client

  • client.global
set(varName, varValue) // 设置全局变量
get(varName) // 获取全局变量
isEmpty // 检查 global 是否为空
clear(varName) // 删除变量
clearAll // 删除所有变量
  • client.test(testName, func) // 创建一个名称为 testName 的测试
  • client.assert(condition, message) // 校验条件 condition 是否成立,否则抛出异常 message
  • client.log(text) // 打印日志

<br/><br/>

response

  • response.body // 字符串 或 JSON (如果content-type 为 application/json.)
  • response.headers
valueOf(headerName) // 返回第一个匹配 headerName 的值,如果没有匹配的返回 null
valuesOf(headerName) // 返回所有匹配 headerName 的值的数组,如果没有匹配的返回空数组
  • response.status // Http 状态码,如: 200 / 400
  • response.contentType
mimeType // 返回 MIME 类型,如:text/plain, text/xml, application/json.
charset // 返回编码 UTF-8 等

<br/><br/><br/>

方法调用示例

GET https://httpbin.org/status/200

> {%
    client.test("Request executed successfully", function() {
        client.assert(response.status === 200, "Response status is not 200");
    });
%}

<br/><br/>

演示接口重构 - 动态更新 Cookie

### 登陆
POST {{host}}/api/login
Content-Type: application/x-www-form-urlencoded

account={{account}}&password={{password}}

> {% client.global.set("Set-Cookie", response.headers.valueOf("Set-Cookie")) %}

### 测试接口
GET {{host}}/api/security/test
Accept: application/json
Cookie: {{Set-Cookie}}

<br/><br/>

RestfulToolkit

RestfulToolkit 同样是个插件,在插件市场搜索安装即可

IDEA 接口调试插件 HTTP Client

安装了这个插件后,打开侧边栏,项目的所有接口信息都会展现在此处:

IDEA 接口调试插件 HTTP Client

我常用的功能就是把指定接口生成的JSON数据拷贝到 HTTP request 文件中,免去手写的麻烦了,你说方便不?

除此之外,使用快捷键 cmd+, 可以根据关键字快速找到接口,回车迅速到达代码接口位置,这也是带来了极大的便利

IDEA 接口调试插件 HTTP Client

<br/><br/><br/>
参考:<br/>
https://www.jetbrains.com/hel...

https://github.com/corningsun...

https://mp.weixin.qq.com/s/bT...

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
7个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
java一生之敌 java一生之敌
2年前
idea下载与设置
一、idea下载下载路径:链接(http://www.jetbrains.com/idea/download/sectionwindows)!图片(https://imghelloworld.osscnbei
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Stella981 Stella981
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Easter79 Easter79
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Wesley13 Wesley13
3年前
IDEA00 IDEA知识点汇总
一、从头搭建IDEA开发环境https://mp.weixin.qq.com/s/6jXHzkU8JfubhDsQJbwl8Q(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fmp.weixin.qq.com%2Fs%2F6jXHzkU8JfubhDsQJbwl8Q)1下
Wesley13 Wesley13
3年前
JavaWeb 调用接口
JavaWeb 如何调用接口CreateTime2018年4月2日19:04:29Author:Marydon1.所需jar包!(https://oscimg.oschina.net/oscnet/0f139
码途织梦者
码途织梦者
Lv1
江汉思归客,乾坤一腐儒
文章
3
粉丝
0
获赞
0