LoginSignup
0
0

More than 1 year has passed since last update.

Ruby の Net::HTTP でリクエストする

Last updated at Posted at 2023-06-10

Ruby 3.0未満

uri = "https://registry.hub.docker.com/v2/library/ubuntu/tags/list"
response = Net::HTTP.get_response(URI.parse(auth_url))

# header指定
# (Net::HTTP.get_responseの第二引数はpathと解釈されるので使えない)
token = "xxxxxx"
response = Net::HTTP.start(url.hostname, url.port, use_ssl: true) do |http|
  req = Net::HTTP::Get.new url
  req['Authorization'] = "Bearer XXXX"
  http.request req
end

use_ssl: trueを書き忘れるとブロックする。ref

Ruby 3.0以降

uri = "https://registry.hub.docker.com/v2/library/ubuntu/tags/list"
response = Net::HTTP.get_response(URI.parse(uri))

# header指定が第二引数でできるようになった
token = "xxxxxx"
response = Net::HTTP.get_response(URI.parse(uri), {"Authorization" => "Bearer XXX"})

楽に書けるようになった。

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