LoginSignup
45
38

More than 3 years have passed since last update.

VS Code拡張「REST Client」の便利機能メモ

Posted at

忘れやすいので、自分用にメモっておく

基本的な情報はココがかなりまとまっている
https://qiita.com/toshi0607/items/c4440d3fbfa72eac840c

Request Variables

API開発しているときに重宝しているREST Clientだが、認証通さないといけない時に、ログインしてToken取得してから各API叩いて確認するのが面倒。
その時に便利なのが、この機能。

@baseUrl = https://example.com/api

# @name login
POST {{baseUrl}}/api/login HTTP/1.1
Content-Type: application/x-www-form-urlencoded

name=foo&password=bar

###

@authToken = {{login.response.headers.X-AuthToken}}

# @name createComment
POST {{baseUrl}}/comments HTTP/1.1
Authorization: {{authToken}}
Content-Type: application/json

{
    "content": "fake content"
}

リクエストの直前にコメントアウトで@nameとして名前をつけたら、そのレスポンス情報を
@authToken = {{login.response.headers.X-AuthToken}}こんな風に取れるので、変数に入れて
以降のAPIコールで使える。
Authorization: {{authToken}}

上記はレスポンスヘッダーからの取得だが、Bodyからの取得もできる。
(例)@address = {{getAddress.response.body.$.address}}

構文
{{requestName.(response|request).(body|headers).(*|JSONPath|XPath|Header Name)}}
45
38
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
38