VSCode延伸套件Rest Client的幾個簡單範例

 因為我在Vistual Studio Code有安裝Rest Client套件,可以很方便地測試Web API,所以記錄一下常用到的幾個範例。


1、建立副檔名為 .http 或 .rest 的文件

2、註解:使用 # 或 // 當作註解

3、如果有兩個以上的API,中間要以「###」來隔開請求,例如

GET http://192.192.192.10/ords_test/demo01
###
GET http://192.192.192.10/ords_test/demo02


4、在Request時,通常會代入上傳的資料,此時資料與HEADER之間需要有空行來區隔,如:

POST http://192.192.192.10/ords_test/demo03
Accept: application/json

{ "text": "Hello, the world!" }


5、上面的範例正好是JSON的格式,下面是以form的方式來Request

POST ttp://192.192.192.10/ords_test/demo04
Content-Type: application/x-www-form-urlencoded

message=TEST


6、在header中加入認證,如下:

GET http://192.192.192.10/ords_test/demo05
Authorization: Bearer ZvABg10ehJxhrNF9Peqknbd76zd6YYGmbYCIBtaW9s6


7、有些Service需要加上Content-Length,其範例如下:

post http://192.192.192.10:8587/ords/ords_test/oauth/token
Content-Type: application/x-www-form-urlencoded
Authorization: Basic LVA3WklzcmRYNEYxcXh==
Content-Length: 29

grant_type=client_credentials


8、windows的vscode按下ctrl+shift+p,並輸入View Request Histor,可檢視Request的記錄

9、利用前綴字「@」來定義變數,使用{{<變數名稱>}}來取得變數值,如下:

@hostname = 192.192.192.10/ords_test/demo06
@id = A100
GET http://{{hostname}}?UID={{id}}


以上是我自己常用到的幾個簡單範例,但是Rest Client還有其他的功能,可自己上網參考

Windows 11安裝時跳過網路連線