How can Koala API requests and replies be recorded?
Asked Answered
F

1

5

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?

Folium answered 22/10, 2011 at 6:52 Comment(0)
C
14

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!

Courteous answered 12/11, 2012 at 9:7 Comment(1)
Very nice, I like the "merge" with the existing DEFAULT_MIDDLEWARE. Thanks!Counterpoise

© 2022 - 2024 — McMap. All rights reserved.