如何记录Koala API请求和回复?

发布于 2024-12-11 02:43:04 字数 176 浏览 0 评论 0原文

我在通过 API 调用与 Facebook 交互的应用程序中使用 Koala。我想在数据库中记录 Koala 生成的原始 HTTP 请求以及 Facebook 发回的响应。我怎样才能抓住这些字符串以便保存它们?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2024-12-18 02:43:04

这是一个老问题,但我自己找不到如何调试 Koala 请求的直接示例。

Koala 使用 Faraday,它具有极强的可扩展性。它基于 Rack 中间件,Koala 为此设置了默认中间件。以下是如何将 STDOUT 日志记录添加到 Faraday 中间件:

# 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

有关 HTTP 请求的 Koala 文档:

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

您可以在此处阅读有关如何使用 Faraday 以及我从何处获取记录器的更多信息:

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

希望这可以帮助其他正在寻找一种简单方法来调试对 Facebook Graph API 的 Koala HTTP 请求的人!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文