Ahmad hamza
Ahmad hamza

Reputation: 1936

How to get the response from Faraday get request with token authentication?

I'm using Faraday with token authentication and unable to configure it to get the response.

So far i'm writing it like this

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

And when i'm using the conn object to get the response it is responding in a different manner like this

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

I'm not sure what i am missing here.

Upvotes: 3

Views: 4540

Answers (1)

Ahmad hamza
Ahmad hamza

Reputation: 1936

Here mentioned how to get response. So i configured it like this for the 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

Also i have moved the token to headers because of when using faraday.token_auth(ENV.fetch('API_TOKEN')) it is adding additional string before the token like token token - 2342342 which is invalid for the api i'm getting the data.

Upvotes: 2

Related Questions