0
0

More than 1 year has passed since last update.

#VsCode + RestClient 拡張で #ruby の API リクエストのコードを自動生成生する ( curl コマンドも )

Last updated at Posted at 2020-04-02

手順

  • RestClient 拡張をインストールしておく
  • VsCode でテキストエディタに以下のように書いて、コマンドパレットで Generate Code Snippet を選ぶ
POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json

{
    "foo": "bar",
    "wow": "yeah"
}

image

生成の例

require 'uri'
require 'net/http'
require 'openssl'

url = URI("https://httpbin.org/post")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["content-type"] = 'application/json'
request.body = "{\"foo\": \"bar\",\"wow\": \"yeah\"}"

response = http.request(request)
puts response.read_body

curl のコマンドを生成する

こちらもコマンドで可能。
クリップボードにコピーされるので、ペーストすればコマンドになる。

image

curl --request POST \
  --url https://httpbin.org/post \
  --header 'content-type: application/json' \
  --data '{"foo": "bar","wow": "yeah"}'

参考

VS Code上でHTTPリクエストを送信し、VS Code上でレスポンスを確認できる「REST Client」拡張の紹介 - Qiita

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

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
0
0