Token Authentication

Simple code

require 'net/http'

url = URI.parse('get json/other data here [link]')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
puts res.body

Just wondering how to put the authentication token in php cURL, I do it like

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer xxx')); //Bearer token for authentication

I wonder how to do this for Ruby.

+4
source share
1 answer

You can simply add a title:

req = Net::HTTP::Get.new(url.to_s)
req['Authorization'] = "Bearer xxx"

or shorter:

request = Net::HTTP::Get.new(url.to_s, {'Authorization' => 'Bearer vF9dft4qmT'})
+9
source

Source: https://habr.com/ru/post/1545775/


All Articles