【问题标题】:How to get the response from Faraday get request with token authentication?如何通过令牌认证获取法拉第 get 请求的响应?
【发布时间】:2018-03-23 06:45:37
【问题描述】:

我正在使用带有令牌身份验证的法拉第,但无法对其进行配置以获取响应。

到目前为止,我是这样写的

conn = Faraday.new(url: url) do |faraday|
 faraday.token_auth(ENV.fetch('API_TOKEN'))
 faraday.adapter Faraday.default_adapter
end

当我使用conn 对象来获取响应时,它会以不同的方式响应,就像这样

conn.response    
ArgumentError (wrong number of arguments (given 0, expected 1+))

我不确定我在这里缺少什么。

【问题讨论】:

    标签: ruby-on-rails ruby faraday


    【解决方案1】:

    Here 提到了如何获得响应。所以我为url配置了这样的:

    url = https://some-api.com/products/1231/other_part_of_url
    base_url = https://some-api.com
    path = products/1231/other_part_of_url
    
    conn = Faraday.new(url: base_url) do |faraday|
     faraday.headers['Authorization'] = ENV.fetch('API_TOKEN')
     faraday.adapter Faraday.default_adapter
    end
    
    response = conn.get(path)
    response.body
    

    由于使用faraday.token_auth(ENV.fetch('API_TOKEN')) 时,我也将令牌移动到标题 它在 token token - 2342342 之类的令牌之前添加了额外的字符串,这对于我正在获取数据的 api 无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      相关资源
      最近更新 更多