Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faraday.get and basic auth #426

Closed
jordansissel opened this issue Oct 23, 2014 · 8 comments
Closed

Faraday.get and basic auth #426

jordansissel opened this issue Oct 23, 2014 · 8 comments

Comments

@jordansissel
Copy link

I can't seem to get Faraday.get to accept the user:pass@host syntax from a url and respect it.

Reproducing with Faraday 0.9.0

Faraday.get("http://foo:bar@localhost:3333/test")

The "server"

% nc -l localhost 3333
GET /test HTTP/1.1
User-Agent: Faraday v0.9.0
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
Connection: close
Host: localhost:3333

I expected an Authentication header, or perhaps an error that auth wasn't supported.

I couldn't find docs on the Faraday.get method on rubydoc.info for the Faraday module: http://www.rubydoc.info/gems/faraday/Faraday - so I'm not sure what I'm doing wrong.

If you point me in the write direction, I am happy to write documentation for this method.

@O-I
Copy link
Contributor

O-I commented Mar 25, 2015

There are several ways you can set authentication headers with Faraday. One way is to initialize a Faraday::Connection instance and use the basic_auth helper method:

conn = Faraday.new(url: 'http://example.com') # create a new Connection with base URL
conn.basic_auth('user', 'pass')               # set the Authentication header
conn.get('/foo')                              # GET http://example.com/foo

You could also use middleware:

conn = Faraday.new(url: 'http://example.com') do |builder|
  builder.use Faraday::Request::Retry
  builder.use Faraday::Request::BasicAuthentication, 'user', 'pass'
  builder.use Faraday::Response::Logger
  builder.use Faraday::Adapter::NetHttp
end

conn.get('/foo')

or:

Faraday.new(url: 'http://example.com') do |builder|
  builder.request  :retry
  builder.request  :basic_authentication, 'user', 'pass'
  builder.response :logger
  builder.adapter  :net_http
end

conn.get('/foo')

Intridea has a nice blog post on basic Faraday usage that's a few years old but still relevant.

Hope it helps some,
O-I

@mislav
Copy link
Contributor

mislav commented Oct 6, 2015

Thanks @O-I for the helpful examples.

@jordansissel For now, please use this form:

conn = Faraday.new('http://example.com')
conn.basic_auth('user', 'pass')
conn.get('/foo')

Sorry for the trouble.

@mislav mislav closed this as completed Oct 6, 2015
@jordansissel
Copy link
Author

Thanks for the pointers, all! It helped me get things working. ❤️❤️❤️

On Tuesday, October 6, 2015, Mislav Marohnić notifications@github.com
wrote:

Thanks @O-I https://github.com/O-I for the helpful examples.

@jordansissel https://github.com/jordansissel For now, please use this
form:

conn = Faraday.new('http://example.com')
conn.basic_auth('user', 'pass')
conn.get('/foo')

Sorry for the trouble.


Reply to this email directly or view it on GitHub
#426 (comment).

@mislav
Copy link
Contributor

mislav commented Oct 7, 2015

This was also reported as #343.

@grosser
Copy link
Contributor

grosser commented Nov 2, 2017

simpler:
Faraday.get('http://localhost:3000', nil, authorization: "Bearer 1123")

@simon1tan
Copy link

simon1tan commented Nov 14, 2021

That did not work, try "Request::BasicAuth"

conn = Faraday.new(url: 'http://example.com') do |builder|
  builder.use Faraday::Request::Retry
  builder.use Faraday::Request::BasicAuth, 'user', 'pass'
  builder.use Faraday::Response::Logger
  builder.use Faraday::Adapter::NetHttp
end

@iMacTia
Copy link
Member

iMacTia commented Nov 15, 2021

@simon1tan the auth middleware recently changed, could you please report this in a separate new issue specifying which version of Faraday you're using and the error message you see, if any?

@jonesnc
Copy link

jonesnc commented Feb 14, 2023

I think the basic_auth method has been renamed to set_basic_auth in more recent versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants