不再須要Postman了!有了它REST請求通通搞定,效率翻倍.md

作開發或者測試,每天可能須要調用REST接口聯調或者測試,查看數據返回是否正確、查看返回的JSON格式、格式化JSON,看似簡單的事情,作起來很費事,並且煩人!看見不少同事使用最多的就是Postman和curl操做了,先看看這兩種操做方式。html

postman發請求

  • 下載postman應用
  • 配置接口,設置參數
  • 多個電腦使用可能還須要使用同一個帳號同步

curl發請求

  • 寫先好curl命令
  • 複製到終端執行
  • 對response JSON格式化

上面兩種方式給個人感受是都很麻煩,接口請求參數多了配置很麻煩、老是在複製來複制去,耗時、跨平臺不方便、不方便保存、不方便分享等等。mysql

指望有一種簡單方便的請求參數配置,跨平臺,方便保存,方便查看請求結果,可以夠集成到多種IDE,方便開發和測試使用。ios

接下來介紹兩大神奇,分別是 VSCodeREST Client和jetbrains家族HTTP Client in IntelliJ IDEA Code Editor,絕不誇張的說,若是你正在使用VSCode或者jetbrains家族的IDE,有了他們讓你的工做效率大大提升,而且你會深深的愛上他。git

在VSCode中使用REST Client

REST Client支持cURLRFC 2616 兩種標準來調用REST API, 使用起來很是簡單,只須要寫一個以.http或者 .rest 結尾命令的的文件便可實現調用。github

首先來看個簡單例子,這裏有一個GET接口(httpbin.org/ip)看看如何調用。web

注意: httpbin.org是一個開源的接口測試網站,它能測試 HTTP 請求和響應的各類信息,好比 cookie、ip、headers 和登陸驗證等,且支持 GET、POST 等多種方法,對 web 開發和測試頗有幫助。httpbin.org/sql

  • 首先在VSCode中建立一個名叫test.http(下載地址:test.http)的文件,而後加入如下代碼
### Get request with a header
GET https://httpbin.org/ip
Accept: application/json

### Get request with a header
curl -H "Content-Type:application/json" -XGET 'https://httpbin.org/ip'
複製代碼

  • 發送請求 發送請求很是簡單,只須要點擊上圖中的Send Request便可執行,最終獲得結果以下

  • 查看結果 執行返回後,HTTP的狀態信息和header都在右側,而且對body已經格式化好了,是否是很是方便,真的是太方便了。

能夠看到上面對同一個接口調用有兩種調用方式,其執行結構都是同樣,即便你寫的再複雜,參數再多的curl請求,拿過來保存在這裏直接執行便可,好比下面這樣一個例子(直接copy個人一個本地測試),直接copy到test.http這個文件中執行便可,而且還可以美觀的查看執行結果。json

curl -H "Content-Type:application/json" -XPUT 'http://localhost:8083/connectors/test-connector/config' -d ' { "connector.class": "io.debezium.connector.mysql.MySqlConnector", "tasks.max": "1", "database.hostname": "localhost", "database.port": "3306", "database.user": "root", "database.password": "password", "database.server.id": "19991", "database.server.name": "trade_order", "database.whitelist": "db_order", "include.schema.changes": "false", "snapshot.mode": "schema_only", "snapshot.locking.mode": "none", "database.history.kafka.bootstrap.servers": "localhost:9092", "database.history.kafka.topic": "dbhistory.trade_order", "decimal.handling.mode": "string", "table.whitelist": "db_order.t_order_item", "database.history.store.only.monitored.tables.ddl":"true", "database.history.skip.unparseable.ddl":"true" }'
複製代碼

經常使用寫法

REST Client的寫法很是簡單,你只須要知道HTTP請求的構成就行,分別是Query StringsRequest HeadersRequest Body,只須要看一個例子就會寫全部的,更復雜的寫法查看REST Client Overview,常見構成結構以下。bootstrap

請求方法 地址
header

request body
複製代碼

例如:bash

### Send POST request with json body
POST https://httpbin.org/post?client=ios&name=哈哈哈
Content-Type: application/json
myHeader: myheader
auth-token: mytoken

{
  "id": 999,
  "value": "content"
}
複製代碼

掌握這一個例子適用於大部分場景就夠了,固然了這裏只是一個介紹,REST Client還支持好多功能,很是優秀,很是好用,簡直是愛不釋手,官網文檔都有REST Client

在IntelliJ中使用HTTP Client in IntelliJ IDEA Code Editor

在中使用HTTP Client in IntelliJ IDEA Code Editor和在VSCode中使用REST Client同樣,惟一的區別就是IntelliJ IDE暫時不支持curl的方式。

附:常見例子

Send POST request with json body

POST https://httpbin.org/post
Content-Type: application/json

{
  "id": 999,
  "value": "content"
}
複製代碼

Send POST request with body as parameters

POST https://httpbin.org/post
Content-Type: application/x-www-form-urlencoded

id=999&value=content
複製代碼

Send a form with the text and file fields

POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="element-name"
Content-Type: text/plain

Name
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data.json"
Content-Type: application/json

< ./request-form-data.json
--WebAppBoundary--
複製代碼

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}}
複製代碼

總結

若是你正在使用VSCode或者IntelliJ IDE必定要使用這兩款優秀的插件,讓你的工做方便省事,方便發送請求,方便查看執行結構,方便保存,方便分享。

相關文章
相關標籤/搜索