Search code examples
facebookhttpkoala

How can Koala API requests and replies be recorded?


I use Koala in an application that interacts with Facebook through API calls. I want to record the raw HTTP requests that Koala generates as well as the responses Facebook sends back in a database. How can I grab these strings so that I can save them?


Solution

  • This is an old question, but I couldn't find a straight-forward example of how to debug Koala requests myself.

    Koala uses Faraday, which is extremely extensible. It's based on Rack middleware, for which Koala sets up default middleware. Here's how to add logging to STDOUT to the Faraday middleware:

    # Overwrite the default middleware Proc (evaluated for each request)
    
    Koala.http_service.faraday_middleware = Proc.new do |builder|
    
      # Add Faraday's logger (which outputs to your console)
    
      builder.use Faraday::Response::Logger
    
      # Add the default middleware by calling the default Proc that we just replaced
      # SOURCE CODE: https://github.com/arsduo/koala/blob/master/lib/koala/http_service.rb#L20
    
      Koala::HTTPService::DEFAULT_MIDDLEWARE.call(builder)
    
    end
    

    Koala documentation about HTTP requests:

    https://github.com/arsduo/koala/wiki/HTTP-Services

    You can read more about how to use Faraday and where I got the logger from right here:

    https://mislav.net/2011/07/faraday-advanced-http/

    Hopefully this helps anyone else looking for a simple way to debug Koala HTTP requests to the Facebook Graph API!