Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http requests with username and password in ruby

from my RoR controller, I'm trying to send http requests that need a username and password.

I tried this

url = 'http://192.168.122.198 ...'
uri = URI.parse(url)
request = Net::HTTP::Get.new(uri.path)
request['Accept'] = 'application/json'
request.body = {'credentials' => {'username' => 'admin', 'password' => 'admin'}}
response = Net::HTTP.get(uri)

but I'm getting this error

Error 401 Unauthorized

Any idea how can I pass the username and password in my request?

like image 216
rh4games Avatar asked Jan 25 '26 00:01

rh4games


1 Answers

Try following

uri = URI.parse("http://192.168.122.198")

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth("admin", "admin")
response = http.request(request)
like image 53
Hardik Avatar answered Jan 26 '26 18:01

Hardik