Search code examples
rubyruby-on-rails-5net-http

Fetch urls prefixed with username:password@


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

Solution

  • 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