I am getting a undefined method `body' error with a faraday call to an API;
URL - https://api.company.com/api/v1/dept/person/query/empid/22435678
I need to pass a username and password through as well. Here is what I am trying. What am I doing wrong?
require 'faraday'
require 'faraday/net_http'
Faraday.default_adapter = :net_http
def show
@response = Faraday.new('https://api.company.com/api/v1/dept/person/query/empid/22435678') do |conn|
conn.request :authorization, :basic, 'testuser', '77hfhncqjwnd'
end
end
CodePudding user response:
You've only set up Faraday object and you're not sending an actual request with it.
# NOTE: configure faraday
api = Faraday.new('https://api.company.com/api/v1/dept/person/query/empid/22435678') do |conn|
conn.request :authorization, :basic, 'testuser', '77hfhncqjwnd'
end
# NOTE: send requests to api `get`, `post` etc.
@response = api.get.body
This is the preferred set up:
api = Faraday.new('https://api.company.com/') do |conn|
conn.request :authorization, :basic, 'testuser', '77hfhncqjwnd'
# conn.response :json # automatically parse responses as json, if needed
end
@response = api.get("api/v1/dept/person/query/empid/22435678").body