Published on

Log Faraday Requests and Response Bodies

Authors

Today I had to dig into an integration to see what request and responses were being sent and received. This ended up being fairly straightforward. The integration connector uses Faraday as its HTTP client. I ended up adjusting the initialization of the Faraday client to include config for the logger as follows:

class SomeServiceConnector

  def initialize
    @conn = Faraday.new(url: Rails.application.secrets.some_api_url) do |faraday|
      faraday.response :logger, ::Logger.new(STDOUT), body: true, bodies: { request: true, response: true }
    end
    @conn.basic_auth(Rails.application.secrets.some_api_username, Rails.application.secrets.some_api_password)
  end
end

Using just :body true did not work in my case. It simply killed the response used further down after logging. I ended up following this suggestion and instead used body: true, bodies: { request: true, response: true }

Sources