Надсилання HTTP-запиту з базовою автентифікацією

Ruby:
Надсилання HTTP-запиту з базовою автентифікацією

How to: (Як це зробити:)

Here’s the simplest way to send an HTTP request with basic authentication using Ruby’s net/http library:

require 'net/http'
require 'uri'

uri = URI('http://example.com/resource')
request = Net::HTTP::Get.new(uri)
request.basic_auth('user', 'password')

response = Net::HTTP.start(uri.hostname, uri.port) do |http|
  http.request(request)
end

puts response.body

Sample output might look like the content of the requested resource:

"Here's your protected resource content!"

Deep Dive (Поглиблено):

Ruby has supported HTTP requests for a long time. Basic auth isn’t the latest trend (more secure alternatives exist, like OAuth), but it remains relevant for simplicity and legacy systems. Behind the scenes, Ruby encodes the credentials and adds an Authorization header to the HTTP request. It’s not super secure (think plain text over HTTP), so use HTTPS to encrypt the transmission. Also, consider storing credentials securely and not hard-coding them.

See Also (Дивіться також):