如何使用适配器防止法拉第改变大写headers
How to use adapter to prevent Faraday from changing capitalize headers
我正在使用 Faraday 创建一个将与 API 交互的 SDK,我需要发送两个 headers API_SIGNATURE 和 API_REQUEST_TIME,所以这是我创建的内容:
class APIClient
def initialize(api_key)
@api_key = api_key
end
def get_users
request.post('/users')
end
private
def request
Faraday.new(@@BASE_API_URL, headers: headers)
end
def headers
timestamp = Time.now.to_i.to_s
return {
'API_SIGNATURE': Digest::MD5.hexdigest(@api_key + timestamp),
'API_REQUEST_TIME': timestamp
}
end
end
出于某种原因,法拉第正在将 API_SIGNATURE
更改为 Api-Signature
,将 API_REQUEST_TIME
更改为 Api-Request-Time
。
我想防止这种情况发生。有人建议使用赞助人:
def request
Faraday.new(@@BASE_API_URL, headers: headers) do |faraday|
faraday.adapter :patron
end
end
但这不起作用。出现以下错误:
/Users/me/.rvm/gems/ruby-2.4.9/gems/patron-0.13.3/lib/patron/session.rb:330:in `handle_request': Operation timed out after 1004 milliseconds with 0 out of 0 bytes received (Faraday::TimeoutError)
from /Users/me/.rvm/gems/ruby-2.4.9/gems/patron-0.13.3/lib/patron/session.rb:330:in `request'
from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/adapter/patron.rb:29:in `call'
from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/rack_builder.rb:143:in `build_response'
from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/connection.rb:387:in `run_request'
from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/connection.rb:175:in `post'
from api.rb:32:in `analyze_text'
from api.rb:38:in `full_analysis'
from api.rb:65:in `<main>'
谢谢。
=======更新==========
问题减少:
headers = { API_SIGNATURE: '', API_REQUEST_TIME: '' }
conn = Faraday.new('https://api.test.io', headers: headers) do |f|
f.adapter :patron
end
puts conn.headers
出现这个错误是因为法拉第默认使用Net::HTTP和Net::HTTP changes the case of your header keys. You can read more about this issue at this related question.
您可以使用 https://lostisland.github.io/faraday/adapters/:
中列出的其他可用适配器之一来解决此问题
- https://lostisland.github.io/faraday/adapters/excon
- https://lostisland.github.io/faraday/adapters/patron
- https://lostisland.github.io/faraday/adapters/httpclient
或需要另一个 gem:
的外部适配器
您对 Patron 的特定实现看起来是正确的,因此请尝试使用其他适配器之一,看看您是否有更好的运气。
更新
我加载了您更新的示例并亲自进行了测试。解决方案是使用字符串化键。您正在使用符号化键。
# symbolized
headers = { API_SIGNATURE: '', API_REQUEST_TIME: '' }
=> {
:API_SIGNATURE => "",
:API_REQUEST_TIME => ""
}
这个returns:
puts conn.headers
{"Api-Signature"=>"", "Api-Request-Time"=>"", "User-Agent"=>"Faraday v0.17.0"}
对比:
# stringified
headers = { 'API_SIGNATURE' => '', 'API_REQUEST_TIME' => '' }
=> {
"API_SIGNATURE" => "",
"API_REQUEST_TIME" => ""
}
现在你得到了正确的值:
puts conn.headers
{"API_SIGNATURE"=>"", "API_REQUEST_TIME"=>"", "User-Agent"=>"Faraday v0.17.0"}
乍看之下,您的原始示例 似乎 已字符串化但并未(这就是我没听清的原因):
{
'API_SIGNATURE': '',
'API_REQUEST_TIME': ''
}
=> {
:API_SIGNATURE => "",
:API_REQUEST_TIME => ""
}
在 Ruby 中为哈希键使用冒号会自动使其成为一个符号,即使它是一个带引号的字符串。您需要对字符串化键使用 hash rocket 语法。
解决方案是使用火箭分配而不是冒号。像这样:
headers = {
'Content-Type': 'application/json',
'x-api-key': '0000'
}
...
conn.headers
=> {"Content-type"=>"application/json", "X-api-key"=>"0000", "User-Agent"=>"Faraday v0.17.3"}
VS
headers = {
'Content-Type' => 'application/json',
'x-api-key' => '0000'
}
conn.headers
=> {"Content-Type"=>"application/json", "x-api-key"=>"0000", "User-Agent"=>"Faraday v0.17.3"}
注意 x 是如何用冒号大写的,而不是如何使用火箭的。
我正在使用 Faraday 创建一个将与 API 交互的 SDK,我需要发送两个 headers API_SIGNATURE 和 API_REQUEST_TIME,所以这是我创建的内容:
class APIClient
def initialize(api_key)
@api_key = api_key
end
def get_users
request.post('/users')
end
private
def request
Faraday.new(@@BASE_API_URL, headers: headers)
end
def headers
timestamp = Time.now.to_i.to_s
return {
'API_SIGNATURE': Digest::MD5.hexdigest(@api_key + timestamp),
'API_REQUEST_TIME': timestamp
}
end
end
出于某种原因,法拉第正在将 API_SIGNATURE
更改为 Api-Signature
,将 API_REQUEST_TIME
更改为 Api-Request-Time
。
我想防止这种情况发生。有人建议使用赞助人:
def request
Faraday.new(@@BASE_API_URL, headers: headers) do |faraday|
faraday.adapter :patron
end
end
但这不起作用。出现以下错误:
/Users/me/.rvm/gems/ruby-2.4.9/gems/patron-0.13.3/lib/patron/session.rb:330:in `handle_request': Operation timed out after 1004 milliseconds with 0 out of 0 bytes received (Faraday::TimeoutError)
from /Users/me/.rvm/gems/ruby-2.4.9/gems/patron-0.13.3/lib/patron/session.rb:330:in `request'
from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/adapter/patron.rb:29:in `call'
from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/rack_builder.rb:143:in `build_response'
from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/connection.rb:387:in `run_request'
from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/connection.rb:175:in `post'
from api.rb:32:in `analyze_text'
from api.rb:38:in `full_analysis'
from api.rb:65:in `<main>'
谢谢。
=======更新==========
问题减少:
headers = { API_SIGNATURE: '', API_REQUEST_TIME: '' }
conn = Faraday.new('https://api.test.io', headers: headers) do |f|
f.adapter :patron
end
puts conn.headers
出现这个错误是因为法拉第默认使用Net::HTTP和Net::HTTP changes the case of your header keys. You can read more about this issue at this related question.
您可以使用 https://lostisland.github.io/faraday/adapters/:
中列出的其他可用适配器之一来解决此问题- https://lostisland.github.io/faraday/adapters/excon
- https://lostisland.github.io/faraday/adapters/patron
- https://lostisland.github.io/faraday/adapters/httpclient
或需要另一个 gem:
的外部适配器您对 Patron 的特定实现看起来是正确的,因此请尝试使用其他适配器之一,看看您是否有更好的运气。
更新
我加载了您更新的示例并亲自进行了测试。解决方案是使用字符串化键。您正在使用符号化键。
# symbolized
headers = { API_SIGNATURE: '', API_REQUEST_TIME: '' }
=> {
:API_SIGNATURE => "",
:API_REQUEST_TIME => ""
}
这个returns:
puts conn.headers
{"Api-Signature"=>"", "Api-Request-Time"=>"", "User-Agent"=>"Faraday v0.17.0"}
对比:
# stringified
headers = { 'API_SIGNATURE' => '', 'API_REQUEST_TIME' => '' }
=> {
"API_SIGNATURE" => "",
"API_REQUEST_TIME" => ""
}
现在你得到了正确的值:
puts conn.headers
{"API_SIGNATURE"=>"", "API_REQUEST_TIME"=>"", "User-Agent"=>"Faraday v0.17.0"}
乍看之下,您的原始示例 似乎 已字符串化但并未(这就是我没听清的原因):
{
'API_SIGNATURE': '',
'API_REQUEST_TIME': ''
}
=> {
:API_SIGNATURE => "",
:API_REQUEST_TIME => ""
}
在 Ruby 中为哈希键使用冒号会自动使其成为一个符号,即使它是一个带引号的字符串。您需要对字符串化键使用 hash rocket 语法。
解决方案是使用火箭分配而不是冒号。像这样:
headers = {
'Content-Type': 'application/json',
'x-api-key': '0000'
}
...
conn.headers
=> {"Content-type"=>"application/json", "X-api-key"=>"0000", "User-Agent"=>"Faraday v0.17.3"}
VS
headers = {
'Content-Type' => 'application/json',
'x-api-key' => '0000'
}
conn.headers
=> {"Content-Type"=>"application/json", "x-api-key"=>"0000", "User-Agent"=>"Faraday v0.17.3"}
注意 x 是如何用冒号大写的,而不是如何使用火箭的。