通常在后端写完接口后,要方便快速的进行测试。
以前喜欢用 PostMan 进行测试。后来发现 IDEA 有更简便的方法。
该方法适合于 jetbrains 全家桶,不局限于 GoLand、PyCharm、IntelliJ IDEA。
文件说明
├── xxx.http # 调用接口的文件
├── http-client.env.json # 通用配置(接口地址等)
├── http-client.private.env.json # 私有配置(密码、Token等)
xxx.http
### 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
{
"dev": {
"host": "https://httpbin.org",
"show_env": "1"
}
}
http-client.private.env.json
{
"dev": {
"username": "",
"password": ""
}
}
示例
GET
### 添加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
### 提交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"
}