0

I am using net-http-persistent gem to fetch pages. It works perfectly fine for most of the cases. But, recently I noted that it returns 401 for urls prefixed with username:password@ e.g. https://username:password@somesite.com. If i try other options like excon/curl they fetch such pages without problem. I saw the logs of the requests made by Net::HTTP::Persistent and found out net::http totally discards the username:password part while connecting to the server.

Can anybody help me how to make Net::HTTP::Persistent make use of username:password@ part.

----------------------EDITED--------------------

Sample code:

url = "https://user:pass@example.com/feed"
uri = URI(url)
http = Net::HTTP::Persistent.new
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request uri
http.shutdown

response.code # yields 401 which it should not as url has username and password.


#Incase of excon, if you do 

response = Excon.get(url)
response.status # yields 200 as it is making use of username:password prefix
mahemoff
  • 44,526
  • 36
  • 160
  • 222
Mohsin Sethi
  • 803
  • 4
  • 16

1 Answers1

3

Based on this issue, try code like:

uri = URI("https://example.com/feed")
req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth 'user', 'pass'

http = Net::HTTP::Persistent.new
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

response = http.request uri, req
http.shutdown

puts response.code
Pavel Mikhailyuk
  • 2,757
  • 9
  • 17
  • Thank you so much for helping me. Although instead of `response = http.request req`, it should be `response = http.request uri, req`. First param should be `uri` and we can pass `request obj` as 2nd param. Source: https://github.com/drbrain/net-http-persistent/issues/27#issuecomment-6564122 – Mohsin Sethi Jun 17 '19 at 11:51