Try   HackMD

VSCode Rest Client 取代 Postman

tags: VSCode Postman Rest Client

使用 Rest Client 來測試 api VS Code 擴充套件

建立設定檔案: {檔案名稱}.http

GET https://localhost:7174/WeatherForecastRedis Accept: application/json { "Username": "test007" }
  • 下圖點擊【GET】上方的【Send Request】就能送出請求
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

使用變數

  • 宣告用 @host 使用則是 {{host}}
@host = https://pwaapi.bacctest.com # login # @name login POST {{host}}/api/Member/login Accept: application/json Content-Type: application/json { "account": "Datw05", "password": "CF79AE6ADDBA60AD018347359BD144D2", "uidKey": "web" } ### @token = {{login.response.body.result.token}} # W1RCGv3 # @name W1RCGv3 POST {{host}}/api/Game/GetGameToken/W1RCGv3 Accept: application/json Content-Type: application/json Authorization: Bearer {{token}} { "device": "web", "lang": "zh-tw", "lobbyURL": "https://wwwpwa.royal-test.com/", "Desk": "1202" } ###
  • 結束時一定要有,不然 @token 會報錯

{ "status": -2, "desc": "Data type is error.", "result": { "$": [ "'@' is invalid after a single JSON value. Expected end of data. Path: $ | LineNumber: 7 | BytePositionInLine: 0." ] } }
  • Headers 如 Content-Type: application/json{ 中間必須有空格,不然會報錯
Header name must be a valid HTTP token ["{"]
  • 有中文亂碼的問題,在 settings.json
  • Ctrl + Shift + P 輸入 Preferences: Open User Settings (JSON)
  • C:\Users\使用者名稱\AppData\Roaming\Code\User\settings.json
"rest-client.decodeEscapedUnicodeCharacters": true,

直接在第一行新增即可解決亂碼問題

參考: 18 - Rest Client - HTTP 請求工具