官方介绍:https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html
创建http请求#
创建临时文件:
按 Ctrl Alt Shift Insert 并选择 HTTP Request。
文件会被创建在项目的Scratches
目录下。
创建非临时文件:
就是指定文件夹创建一个文件,这个文件就可以跟随项目的git一块走。
HTTP Client 特性#
HTTP 请求存储在以.http
或.rest
为后缀的文件中,并且带有 API
小图标。
请求文件可以包含多个请求,多个请求中间用3个井号 ###
隔开;如果是临时文件,每次执行请求后,会在请求下方生成对应请求结果的文件链接,按住 Ctrl + 鼠标左键
可以打开。
所有的请求结果,请求历史记录,cookies等信息会存放在 .idea
文件夹下,如下:
编写 HTTP 请求#
使用idea的Live templates
。
gtr:get请求模板
mptr:post请求
可以按 Ctrl+J
查看可用模板的列表。
请求方式#
GET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
### GET 请求带请求头
GET https://httpbin.org/ip
Accept: application/json
### GET 请求带参数
GET https://httpbin.org/get?show_env=1
Accept: application/json
### GET 请求带环境变量
GET {{host}}/get?show_env={{show_env}}
Accept: application/json
### GET 请求,禁用重定向
# @no-redirect
GET http://httpbin.org/status/301
### GET 请求,动态变量
GET http://httpbin.org/anything?id={{$uuid}}&ts={{$timestamp}}
###
|
POST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
### post请求,json请求体
POST https://httpbin.org/post
Content-Type: application/json
{
"id": 999,
"value": "content"
}
### post请求,表单
POST https://httpbin.org/post
Content-Type: application/x-www-form-urlencoded
id=999&value=content
### post请求,表单,字段和文件一块发送
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--
### post请求,请求主体中使用动态变量
POST https://httpbin.org/post
Content-Type: application/json
{
"id": {{$uuid}},
"price": {{$randomInt}},
"ts": {{$timestamp}},
"value": "content"
}
###
|
PUT
1
2
3
4
|
PUT http://localhost:8080/person/put
Content-Type: application/json
{"name": "陈皮","age": 17}
|
PATCH
1
2
3
4
5
|
###
PATCH http://localhost:8080/person/put
Content-Type: application/json
{"name": "陈皮","age": 17}
|
鉴权方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
### Basic authorization.
GET https://httpbin.org/basic-auth/user/passwd
Authorization: Basic user passwd
### Basic authorization with variables.
GET https://httpbin.org/basic-auth/user/passwd
Authorization: Basic {{username}} {{password}}
### Digest authorization.
GET https://httpbin.org/digest-auth/realm/user/passwd
Authorization: Digest user passwd
### Digest authorization with variables.
GET https://httpbin.org/digest-auth/realm/user/passwd
Authorization: Digest {{username}} {{password}}
### Authorization by token, part 1. Retrieve and save token.
POST https://httpbin.org/post
Content-Type: application/json
{
"token": "my-secret-token"
}
> {% client.global.set("auth_token", response.body.json.token); %}
### Authorization by token, part 2. Use token to authorize.
GET https://httpbin.org/headers
Authorization: Bearer {{auth_token}}
###
|
断言方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
### Successful test: check response status is 200
GET https://httpbin.org/status/200
> {%
client.test("Request executed successfully", function() {
client.assert(response.status === 200, "Response status is not 200");
});
%}
### Failed test: check response status is 200
GET https://httpbin.org/status/404
> {%
client.test("Request executed successfully", function() {
client.assert(response.status === 200, "Response status is not 200");
});
%}
### Check response status and content-type
GET https://httpbin.org/get
> {%
client.test("Request executed successfully", function() {
client.assert(response.status === 200, "Response status is not 200");
});
client.test("Response content-type is json", function() {
var type = response.contentType.mimeType;
client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'");
});
%}
### Check response body
GET https://httpbin.org/get
> {%
client.test("Headers option exists", function() {
client.assert(response.body.hasOwnProperty("headers"), "Cannot find 'headers' option in response");
});
%}
###
|
环境变量#
在 Spring Boot 项目中,我们通过 Profile 机制,实现不同环境,不同配置文件。在 IDEA HTTP Client 插件中,提供类似的机制,可以定义配置文件,抽取出不同环境的变量。
注意:文件名http-client.env.json
不能更改!改了idea无法识别!
- 通过创建
http-client.env.json
配置文件,定义通用变量。例如说,url 地址、port 端口等等。
- 通过创建
http-client.private.env.json
配置文件,定义敏感变量。例如说,username/password 账号密码、token 访问令牌等等。
🔥 注意再注意,http-client.private.env.json
是定义定义敏感变量的配置文件,所以不要提交到 Git 仓库中!!!
下面,我们来简单使用下。创建配置文件,如下图所示:

测试环境变量:

结果断言#
IDEA HTTP Client 提供 Response Handler Script 机制,允许我们通过编写 JavaScript 脚本,进行响应结果的处理。
同时,它又提供了断言函数,这样它不仅仅能够作为一个 HTTP API 的调用工具,还能作为自动化测试工具。
下面,我们来简单使用下。创建一个新的 .http
文件,如下图所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
### 001 测试 /user/login:登陆成功
POST http://127.0.0.1:8080/user/login
Content-Type: application/x-www-form-urlencoded
username=yudaoyuanma&password=123456
> {%
client.test("验证登陆成功", function (){
client.assert(response.status === 200, "响应状态应该是 200,结果是 " + response.status)
client.assert(response.body.userId === 1, "响应的 userId 应该是 1,结果是 " + response.body.userId)
client.assert(response.body.token === "token001", "响应的 token 应该是 token001,记过是 " + response.body.token)
});
%}
### 002 测试 /user/login:登陆失败,密码不正确
POST http://127.0.0.1:8080/user/login
Content-Type: application/x-www-form-urlencoded
username=yudaoyuanma&password=buzhidao
> {%
client.test("验证登陆失败", function (){
client.assert(response.status === 200, "响应状态应该是 200,结果是 " + response.status)
});
%}
|
下面,我们来简单测试下。
① 执行 001 测试,通过。如下图所示:

② 执行 002 测试,不通过。如下图所示:

结果暂存与使用#
IDEA HTTP Client 提供了环境变量的设置函数,这样配合 Response Handler Script 机制,可以实现响应结果的暂存与使用的功能。
例如说,我们可以先调用登陆接口,接响应结果中的 token
暂存到环境变量中。然后,在调用其它接口时,就可以带上暂存到环境变量中的 token
啦。
下面,我们来简单使用下。创建一个新的 .http
文件,如下图所示:

1
2
3
4
5
6
7
|
{
"timestamp": "2020-12-19T17:35:58.033+0000",
"status": 500,
"error": "Internal Server Error",
"message": "小朋友,你没有登录哟!",
"path": "/user/get-current"
}
|
报错,因为在环境变量的 token_from_server
不存在,获取不到用户信息。
② 然后,调用 /user/login
接口,进行登陆,返回结果如下:
1
2
3
4
|
{
"userId": 1,
"token": "token001"
}
|
将响应结果的 token
设置到环境变量的 token_from_server
中。
③ 最后,调用 /user/get-current
接口,返回结果如下:
1
2
3
4
5
|
{
"gender": 1,
"nickname": "芋道源码",
"id": 1
}
|
成功,说明此时环境变量的 token_from_server
存在。
环境变量存储在内存中,重启 IDEA 后失效。