IDEA HTTP Client 使用教程

IDEA HTTP Client Editor 工具使用教程

本教程针对 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 fields
  • Code folding for requests, their parts, and response handler scripts
  • Inline documentation for request header fields and doc tags
  • Viewing a structure of HTTP requests
  • Language injections inside the request message body
  • Move 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

###

环境配置

完成一下几步操作即可根据配置选择运行环境

  • 新建两个文件:http-client.env.json http-client.private.env.json
  • 配置 http-client.env.json
{
    "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"
    }
}
  • 配置 http-client.private.env.json
{
    "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}}
 
###

骚操作

  • 文件上传时注意 filename 为全部小写,不然会无法上传文件,示例如下:180.jpg文件将会无法上传
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--

参考文献

  • 官方文档
  • IDEA中的HTTP Client Editor测试API

你可能感兴趣的:(IDEA,工具使用,开发工具,intellij,idea,httpwebrequest,单元测试,java)