本教程针对 IDEA 使用者,主要是用来进行 api 接口测试,省去另外开 postman 或者相同工具的麻烦,
编写好的文件可以直接发给测试人员使用。也可以使用 IDEA 插件 restfulToolkit 或者直接写单元测试。
示个人请款以及喜好来选择。
官方文档:HTTP client in IntelliJ IDEA code editor
引用官方文档对 HTTP client 工具的功能、特性进行说明
功能:When testing a web service, you can create, edit, and execute HTTP Requests directly in the IntelliJ IDEA code editor.
IDEA 对 http files 提供的特性:
Code highlighting
Code completion
for hosts, method types, and header fieldsCode folding
for requests, their parts, and response handler scriptsInline documentation
for request header fields and doc tagsViewing a structure
of HTTP requestsLanguage injections
inside the request message bodyMove refactorings
Live templates
IDEA 对 .http/.rest 文件生成请求提供了一下几种缩写
fptr
快速生成文件上传请求POST http://localhost:80/api/item
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="field-name" filename="file.txt"
< ./relative/path/to/local_file.txt
--WebAppBoundary--
gtr
快速生成 GET 请求GET http://localhost:80/api/item
Accept: application/json
gtrp
快速生成 GET 请求,带参数GET http://localhost:80/api/item?id=99
Accept: application/json
mptr
快速生成表单请求POST http://localhost:80/api/item
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="field-name"
field-value
--WebAppBoundary--
ptr
快速生成 POST 请求POST http://localhost:80/api/item
Content-Type: application/json
{}
ptrp
快速生成 POST 请求POST http://localhost:80/api/item
Content-Type: application/x-www-form-urlencoded
id=99&content=new-element
###
完成一下几步操作即可根据配置选择运行环境
{
"development": {
"host": "localhost",
"id-value": 12345,
"username": "",
"password": "",
"my-var": "my-dev-value"
},
"production": {
"host": "example.com",
"id-value": 6789,
"username": "",
"password": "",
"my-var": "my-prod-value"
}
}
{
"development": {
"username": "dev-user",
"password": "dev-password"
},
"production": {
"username": "user",
"password": "password"
}
}
GET http://{{host}}/api/json/get?id={{id-value}}&key={{unresolved_var}}
Authorization: Basic {{username}} {{password}}
Content-Type: application/json
{
"key": {{my-var}}
}
### 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}}
###
POST http://{{host}}:{{port}}/file/upload
Content-Type: multipart/form-data; boundary=myfield
--myfield--
Content-Disposition: form-data; name="file"; filename=90.jpg
< E:\Documents\image\90.jpg
--myfield--
Content-Disposition: form-data; name="file2"; fileName=180.jpg
< E:\Documents\image\180.jpg
--myfield--
Content-Disposition: form-data; name="fileGroup"
0
--myfield--
Content-Disposition: form-data; name="eventId"
0
--myfield--