Reputation: 804
I have a URL which points to JSON output, however it requires a username and password through a standard Apache pop-up authentication box. I tried to access this URL from my Rails controller:
result = JSON.parse(open("http://user:[email protected]/json").read)
However it isn't working. I have the following at the top of my controller:
require 'open-uri'
require 'json'
I just get an error which says "ArgumentError, userinfo not supported. [RFC3986]".
I tried also the Basic Auth code, however it doesn't work since I'm using Digest Auth:
open("http://...", :http_basic_authentication=>[user, password])
Upvotes: 1
Views: 914
Reputation: 1490
I would use HTTPClient for this. It's another dependency but it makes RESTful requests with digest authentication super easy.
require "httpclient"
# foundational parts of your URI
base = "http://myurl.com"
# create a new http connection instance
http = HTTPClient.new
# set authentication on the connection
http.set_auth(base, "user", "pass")
# make your request
response = http.get("#{base}/json")
# parse the response's JSON into a Hash
JSON.parse response.body
Upvotes: 1