Henry Yang
Henry Yang

Reputation: 2583

How to access the page protected by basic auth using Faraday?

I have a php page I want to access and that page is protected by basic auth. I know the url and username/password, they are listed below in code:

url = 'https://henry.php' # note that it is a php website
username = 'foo'
password = 'bar'

Faraday provide basic auth function, their doc says that I should use the following code:

connection = Faraday.new(url: url) do |conn|
  conn.basic_auth(username, password)
end 

I want to get the response body of the above url to make sure that the basic auth indeed succeed and I can access the content, but I don't know how to. I tried each of the following ways but none of them work:

connection.body
connection.response.body
connection.env.response.body

# or

r = connection.get
r.body
r.response.body
r.env.response.body

# or

r = connection.get '/'
r.body
r.response.body
r.env.response.body

What is the proper way to get the body?

Note:

In browser, I access https://henry.php directly and browser prompt me a box asking my username and password and I enter them and I can see the content - I can see the details I have is correct and it should work (this is because browser knows how to do basic auth), but I just can't figure out how to do it in code using Faraday.

Upvotes: 3

Views: 6885

Answers (4)

Carlos Daniel
Carlos Daniel

Reputation: 91

Faraday 2.x

You can use:

Faraday.new(...) do |conn|
  conn.request :authorization, :basic, 'username', 'password'
end

source: https://lostisland.github.io/faraday/middleware/authentication

Upvotes: 3

t56k
t56k

Reputation: 6981

For Faraday 1.x this is actually different:

Faraday.new(...) do |conn|
  conn.request :basic_auth, 'username', 'password'
end

Upvotes: 0

Adif_Sgaid
Adif_Sgaid

Reputation: 79

The Faraday gem comes with a number of plugins (middleware) that make HTTP requests simpler and more customizable. In Ruby, basic authentication might be difficult. Let's have a look at it;

require "faraday"

request_helper = Faraday.new(url: 'example.com') do |builder|
                builder.use Faraday::Request::BasicAuthentication, client_key, secret_key
             end

# you make HTTP requests using `request_helper` since basic auth is configured

response = request_helper.get('/myendpoit')

You must obtain tokens by proving client and secret keys when using an API such as the Stripe API. We can give client and secret keys as inputs to the Faraday::Request::BasicAuthentication middleware to establish basic authentication using the same approach as before.

Upvotes: 0

Henry Yang
Henry Yang

Reputation: 2583

Answering my own question:

Instead of just:

connection = Faraday.new(url: url) do |conn|
  conn.basic_auth(username, password)
end

you should remember to use an adapter:

connection = Faraday.new(url: url) do |conn|
  conn.adapter Faraday.default_adapter # make requests with Net::HTTP
  conn.basic_auth(username, password)
end

because Faraday is an interface, it does not do the actual work of making connection and request, the adapter does that, so you need it for it to work.

Then, to get ther response body you want, you can just:

response = connection.get
response.body

Upvotes: 8

Related Questions