I can't seem to get Faraday.get
to accept the user:pass@host syntax from a url and respect it.我似乎无法让Faraday.get
接受来自 url 的user:pass@host语法并尊重它。
Reproducing with Faraday 0.9.0使用 Faraday 0.9.0 进行复制
Faraday.get("http://foo:bar<strong i="9">@localhost</strong>: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.我在 rubydoc.info 上找不到有关 Faraday 模块的Faraday.get
方法的文档: http ://www.rubydoc.info/gems/faraday/Faraday - 所以我不确定我是什么做错了。
If you point me in the write direction, I am happy to write documentation for this method.如果您指出我的写作方向,我很乐意为此方法编写文档。
enThere are several ways you can set authentication headers with Faraday.您可以通过多种方式使用 Faraday 设置身份验证标头。 One way is to initialize a Faraday::Connection
instance and use the basic_auth
helper method:一种方法是初始化一个Faraday::Connection
实例并使用basic_auth
辅助方法:
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. Intridea 有一篇关于法拉第基本用法的不错的博客文章,该文章已有几年历史,但仍然具有相关性。
Hope it helps some,希望对一些人有帮助
OI油耗
Thanks @OI for the helpful examples.感谢@OI 提供有用的示例。
@jordansissel For now, please use this form: @jordansissel现在,请使用此表格:
conn = Faraday.new('http://example.com')
conn.basic_auth('user', 'pass')
conn.get('/foo')
Sorry for the trouble.抱歉,添麻烦了。
enThanks for the pointers, all!谢谢大家的指点! It helped me get things working.它帮助我让事情正常进行。 ❤️❤️❤️ ❤️❤️❤️
On Tuesday, October 6, 2015, Mislav Marohnić [email protected] 2015 年 10 月 6 日,星期二,Mislav Marohnić [email protected]
wrote:写道:
enThanks @OI https://github.com/OI for the helpful examples.感谢@OI https://github.com/OI提供有用的示例。
@jordansissel https://github.com/jordansissel For now, please use this @jordansissel https://github.com/jordansissel现在,请使用这个
form:形式:conn = Faraday.new('http://example.com') conn = Faraday.new('http://example.com')
conn.basic_auth('user', 'pass') conn.basic_auth('用户', '通过')
conn.get('/foo') conn.get('/foo')Sorry for the trouble.抱歉,添麻烦了。
— —
Reply to this email directly or view it on GitHub直接回复此邮件或在 GitHub 上查看
https://github.com/lostisland/faraday/issues/426#issuecomment -145914646. https://github.com/lostisland/faraday/issues/426#issuecomment -145914646。
这也被报告为#343。
en更简单:
Faraday.get('http://localhost:3000', nil, authorization: "Bearer 1123")
最有用的评论
更简单:
enFaraday.get('http://localhost:3000', nil, authorization: "Bearer 1123")