36

How do I log requests being sent with with httparty?

HTTParty.post(
          @application_url,
          :headers => {
            "Accept"        => "application/json",
            "Content-Type"  => "application/json; charset=utf-8"
          },
          :body => {
            "ApplicationProfileId" => application.applicationProfileId
          }.to_json
        )
Eric Francis
  • 23,039
  • 31
  • 88
  • 122

3 Answers3

70

Use debug_output at the class level:

class Foo
  include HTTParty
  debug_output $stdout
end

or per request

response = HTTParty.post(url, :body => body, :debug_output => $stdout)
Brian Low
  • 11,605
  • 4
  • 58
  • 63
  • 1
    Nice, nobody else seems to know how to do it per request – Jaco Pretorius Feb 12 '16 at 15:42
  • 8
    Watch out: If you dig a little deeper into the Net::HTTP library it's based onto, you'll realize this should never be used in production https://ruby-doc.org/stdlib-2.3.1/libdoc/net/http/rdoc/Net/HTTP.html#method-i-set_debug_output – hatenine Nov 14 '16 at 13:30
  • 1
    So i would recommend: `SomeLogger.info(HTTParty.get(uri, query: parameters).request.last_uri)` – hatenine Nov 15 '16 at 09:17
6

You ca use a built-in logger:

my_logger = Rails.logger || Logger.new # use what you like
my_logger.info "The default formatter is :apache. The :curl formatter can also be used."
my_logger.info "You can tell which method to call on the logger too. It is info by default."

HTTParty.get "http://google.com", logger: my_logger, log_level: :debug, log_format: :curl
Dende
  • 545
  • 6
  • 19
4

Use the class-level debug_output method to set an output stream where debugging information gets sent.

user229044
  • 232,980
  • 40
  • 330
  • 338