通常在后端写完接口后,要方便快速的进行测试。
以前喜欢用 PostMan
进行测试。后来发现 IDEA
有更简便的方法。
该方法适合于 jetbrains
全家桶,不局限于 GoLand
、PyCharm
、IntelliJ IDEA
。
文件说明
1 2 3
| ├── xxx.http # 调用接口的文件 ├── http-client.env.json # 通用配置(接口地址等) ├── http-client.private.env.json # 私有配置(密码、Token等)
|
xxx.http
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| ### get请求 GET https://httpbin.org/ip={{host}} Accept: application/json
### post表单 POST https://httpbin.org/post Content-Type: application/x-www-form-urlencoded
id=999&value=content
### postJson POST https://httpbin.org/post Content-Type: application/json
{ "id": 999, "value": "content" }
|
http-client.env.json
1 2 3 4 5 6
| { "dev": { "host": "https://httpbin.org", "show_env": "1" } }
|
http-client.private.env.json
1 2 3 4 5 6
| { "dev": { "username": "", "password": "" } }
|
示例
GET
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ### 添加header GET https://httpbin.org/get?show_env=1 Accept: application/json
### 使用配置文件中的值 GET {{host}}/get?show_env={{show_env}} Accept: application/json
### 禁止请求重定向 # @no-redirect GET http://httpbin.org/status/301
### 简单的动态变量(内置的) 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
| ### 提交JSON数据 POST https://httpbin.org/post Content-Type: application/json
{ "id": 999, "value": "content" }
### 提交表单 POST https://httpbin.org/post Content-Type: application/x-www-form-urlencoded
id=999&value=content
### 上传文本和json文件 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 https://httpbin.org/post Content-Type: application/json
{ "id": {{$uuid}}, "price": {{$randomInt}}, "ts": {{$timestamp}}, "value": "content" }
|