IDEA使用自带Http Client替换Postman校验接口(Idea uses its own HTTP client to replace the postman verification interface)

对比Postman的优势

对于数据格式变动可以更为敏锐的观察到。生成的接口请求文件可以同步到代码库,支持多人使用。

使用说明

创建请求文件

使用IDEA,在项目的Scratches下创建Http Request文件,其中请求用###进行分割。

生成环境变量文件

http-client.env.json 共有环境变量

这个文件中主要区分环境、共有信息等数据,例如:

{
  "dev": {
    "url": "localhost:8080/abc"
  },
  "prod": {
    "url": "192.168.1.1:8080/abc"
  }
}

http-client.private.env.json 私有环境变量

这个文件主要是个人个性化配置,一般不需要提交到代码库,例如:

{
  "dev": {
    "userName": "aaa"
  },
  "prod": {
    "userName": "bbb"
  }
}

IDEA自动生成文件

在使用过程中发现IDEA项目空间的.idea/httpRequests目录下,会生成这样3类文件

http-requests-log.http 请求历史记录

其中记录了通过IDEA调用的所有请求记录信息,例如:

POST http://localhost:8080/abc/getName
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

<> 2021-11-23T083715.200.json

###

POST http://localhost:8080/abc/getName
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

<> 2021-11-23T083629.200.json

###

http-client.cookies Cookies信息

通过这个文件的信息,请求可以根据域名直接携带Cookies,例如:

# domain	path	name	value	date
localhost	/abc	jsessionid	1234567890	-1

这样对比请求内容来说,就不用在请求头中增加Cookies:内容。

XXX.json 请求结果

在上面例子中的<> 2021-11-23T083629.200.json就是对应生成的响应结果文件。

响应结果编码处理

因为功能支持远程JavaScript,可以对响应结果进行代码处理,包括值赋值环境变量等。

###
POST http://{{url}}/abc/getName

> {%
client.test("脚本", function (){
    client.log("test log");
    client.log(response.status);
    client.log(response.body.records);
});
%}

<> 2021-11-23T110554.200.json

###

巨人的肩膀

芋道 Spring Boot API 接口调试 IDEA HTTP Client | 芋道源码 —— 纯源码解析博客 (iocoder.cn)

————————

对比Postman的优势

Changes in data format can be observed more acutely. The generated interface request file can be synchronized to the code base and can be used by multiple people.

instructions

Create request file

Using idea, create an HTTP request file under the scratches of the project, in which the request is split by ###.

Generate environment variable file

http-client.env.json 共有环境变量

This file mainly distinguishes data such as environment and common information, such as:

{
  "dev": {
    "url": "localhost:8080/abc"
  },
  "prod": {
    "url": "192.168.1.1:8080/abc"
  }
}

http-client.private.env.json 私有环境变量

This file is mainly personal personalized configuration and generally does not need to be submitted to the code base, for example:

{
  "dev": {
    "userName": "aaa"
  },
  "prod": {
    "userName": "bbb"
  }
}

Idea automatically generates files

During use, it is found that such three types of files will be generated in the. Idea / httprequests directory of the idea project space

http-requests-log.http 请求历史记录

It records all request record information called through idea, for example:

POST http://localhost:8080/abc/getName
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

<> 2021-11-23T083715.200.json

###

POST http://localhost:8080/abc/getName
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

<> 2021-11-23T083629.200.json

###

http-client.cookies Cookies信息

Through the information of this file, the request can directly carry cookies according to the domain name, for example:

# domain	path	name	value	date
localhost	/abc	jsessionid	1234567890	-1

In this way, compared with the request content, there is no need to add cookies in the request header.

XXX.json 请求结果

< strong > & lt& gt; 2021-11-23t083629.200. JSON < / strong > is the corresponding generated response result file.

Response result encoding processing

Because the function supports remote JavaScript, you can code the response results, including value assignment, environment variables, etc.

###
POST http://{{url}}/abc/getName

> {%
client.test("脚本", function (){
    client.log("test log");
    client.log(response.status);
    client.log(response.body.records);
});
%}

<> 2021-11-23T110554.200.json

###

Shoulders Of Giants

Taro spring boot API interface debugging idea HTTP client | taro source code – pure source code analysis blog (iocoder. CN)