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--

參考文獻

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章