IDEA rest-client,會了它我還沒打開過postman

1. 簡介

​ IDEA自己是提供測試接口工具的, rest-client/http client,功能很強大,在我平常使用中,徹底能夠作到不須要打開 postman 的程度。 入口以下:json

image.png

<center>圖一 打開方式</center>api

​ 點擊 Test RESTful Web Service 是打開 Rest Client 工具,能夠理解爲一個使用了IDEAUI的接口測試工具,相對比較可視化,直接在上面根據實際需求填充值便可,再點左上角的按鈕啓動測試。app

image.png

<center>圖二 rest client</center>工具

​ 圖一選項中的第二個選項 Open Http Requests Collection 纔是 今天的重點,能夠直接經過 .http 後綴的文件來請求接口。除了在工具欄進去,也能夠在任何地方本身 new 一個出來(圖4),或者點擊圖二中黃色底的 Convert request to the new format ,也會在 Scratches and consoles 下的 Scratches文件夾 生成一個 .http文件(圖五)post

image.png

<center>圖三 http client</center>測試

image.png

<center> 圖四</center>url

image.png

<center>圖五 </center>spa

2. http client

​ 下面主要講一下 http client 的使用,簡單理解就是 http請求的參數,均可以在這裏自行定義。3d

​ 剛剛進入文件會給你一個例子,第一行很容易理解,選擇請求方式(GET、POST、PUT等) 以及 請求地址,這是必填項;第二行開始並不必定是要有的,不給的話就根據你的請求方式按照默認參數來給rest

GET http://localhost:8080/resources
Accept: */*
Cache-Control: no-cache

2.1 普通請求

​ 請求成功以後會在在下方控制檯顯示請求的內容,以及結果,並在 .http 文件的對應請求下方留有一條歷史記錄,方便查看(如圖六)

image.png

<center>圖六 http client運行效果</center>

2.2 變量請求

​ 系統常常會部署在不一樣的環境之中,http client 同時也支持根據不一樣的環境修改參數,支持的參數有:

  1. 請求ip,端口和路徑
  2. 查詢參數或值
  3. 請求頭的值
  4. 在外部文件爲請求體提供任意值

​ 支持的環境變量定義文件有具體的命名方式,而且須要跟 .http文件在同一目錄下(Scratches and consoles文件夾下面的只能普通請求),命名也有要求,須要是下面的其中之一:

  • rest-client.env.json
  • http-client.env.json
  • rest-client.private.env.json
  • http-client.private.en.json

​ 上述文件中帶private的文件不表示是一個私人的文件,可能包括密碼,令牌,證書等敏感信息,默認狀況下,此文件被添加到VCS忽略文件列表中。使用以下(內容是 Json 格式):

image.png

<center>圖七 環境變量文件設置</center>

image-20201107175323436

<center>圖八 運行時選擇</center>

​ 如圖七 圖八,在設置完環境變量以後運行時就能夠選擇相關的環境變量傳入請求中,直接運行便可

3. 使用小技巧

3.1 語法

  1. 註釋使用 // 或者 #
  2. 分開請求用 ###
  3. 若是不想要生成日誌
  4. 腳本 以>開頭,將邏輯寫在 {% %} 中,能夠調用client對象

image.png

<center>圖九 不記錄日誌</center>

3.2 快捷鍵

快捷鍵 說明
gtr 生成一個get請求
gtrp 生成一個get請求,外加參數
ptr 生成一個post請求,格式是application/json
ptrt 生成一個post請求,格式是application/x-www-form-urlencoded
mptr 生成一個post請求,表單提交
fptr 生成一個post請求,文件上傳
###
# gtr
GET http://localhost:80/api/item
Accept: application/json

### 
# gtrp 
GET http://localhost:80/api/item?id=99
Accept: application/json

###
# prt
POST http://localhost:80/api/item
Content-Type: application/json

{}

###
#ptrp
POST http://localhost:80/api/item
Content-Type: application/x-www-form-urlencoded

id=99&content=new-element

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

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

###

3.3 腳本

​ 能夠在請求中添加腳本,打印或者存儲一些信息,能夠使用腳本存儲一些結果變量,而後相似環境變量同樣的引用。

image.png

<center>圖十 腳本示例</center>

​ 若是使用了腳本的打印,結果控制檯會出現一個選項框,顯示打印內容,以下方的 Respinse Handler

image.png

3.4 多看示例

​ IDEA在 .http 文件的右邊已經給咱們提供了不少例子,以及便捷操做,當本身無從下手的時候,也能夠參考一下。還提供了從 cURL 轉換爲 .http 文件可識別的請求功能。

image.png

本文由博客一文多發平臺 OpenWrite 發佈!
相關文章
相關標籤/搜索