Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 851
TimeEmit Avatar asked Oct 22 '11 06:10

TimeEmit


1 Answers

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!

like image 161
joelvh Avatar answered Oct 10 '22 05:10

joelvh