前言
我們大家在日常開發過程中,或多或少都涉及到 api 接口的測試。例如,有的小伙伴使用 chrome 的 postman 插件,或者使用火狐的 restclient 等工具。事實上,這些工具是測試 api 接口非常有效的方式之一,筆者之前也一直使用 postman 完成 api 接口的測試工作。今天,筆者推薦另外一個非常好用的小工具,能夠幫助讀者快速測試 api 接口。這個工具就是 idea 的 editor rest client。
idea 的 editor rest client 在 intellij idea 2017.3 版本就開始支持,在 2018.1 版本添加了很多的特性。事實上,它是 intellij idea 的 http client 插件。
開始工作
首先,我們可以在任意目錄下創建一個 xxx.http 文件,如圖所示。
這里,我們需要使用 ### 進行 http 請求分割,并在后面添加注釋,案例如下所示。
1
2
3
4
5
6
|
### 用戶登錄 post http: //localhost:8088/oauth/token?grant_type=password&username=lgz&password=123456 accept : application/json content-type : application/json;charset=utf- 8 authorization: basic client secret cache-control : no-cache |
因此,我們獲得的響應內容。
多環境配置
在開發過程中,我們通常會存在多套環境,例如開發環境、測試環境、預發環境、生產環境 等。因此,如果 editor rest client 能夠像 postman 一樣做到多環境配置就太棒了。事實上,editor rest client 已經支持了這個特性,我們只需要創建 rest-client.env.json 文件,并且配置多環境信息即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
{ "development" : { "url" : "http://localhost:8088" , "token" : "bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c" }, "test" : { "url" : "http://localhost:8089" , "token" : "bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c" }, "preproduction" : { "url" : "http://activity.720ui.com" , "token" : "bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c" }, "product" : { "url" : "http://activity.720ui.com" , "token" : "bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c" } } |
此時,改造之前的 url,將 http://localhost:8088 改造成 url 代替。
1
|
post /oauth/token?grant_type=password&username=lgz&password= 123456 |
這里,我們獲得的整體效果。
案例詳解
現在,我們來寫一個完整的案例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
### 創建用戶信息 post /v1/m/users accept : application/json content-type : application/json;charset=utf- 8 authorization: { "username" : "xiaoyue" , "realname" : "小岳" , "password" : "111111" , "tel" : "18305930000" , "weixin" : "lianggzone" , "sex" : 1 } ### 修改用戶信息 put /v1/m/users/ 723181 accept : application/json content-type : application/json;charset=utf- 8 authorization: cachepatch-control : no-cache { "username" : "xiaoyue" } ### 查詢用戶信息 get /v1/c/users/lgz/username accept : application/json content-type : application/json;charset=utf- 8 authorization: ### 查詢用戶信息列表 get /v1/c/users?keyword=梁 accept : application/json content-type : application/json;charset=utf- 8 authorization: |
參考資料
https://marketplace.visualstudio.com/items?itemname=humao.rest-clientt
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://blog.720ui.com/2018/restclient_use/