Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the request body of a SOAP request with Savon (Ruby on Rails)

I'm using Savon gem in Ruby on Rails to communicate with a wsdl WS. Everything is working fine, but I want to log the request XML using a custom log, i.e. not Rails or Savon logger. My code looks something like this:

    response = self.client.request :add_order do
      soap.body = { 
        :indata => {
          "CustomerNo" => config[:kundnr],
          "Pwd" => config[:password],
          "OrderDate" => order["purchase_order_date"].strftime('%Y%m%d')
        }
      }
    end

Accessing the response is no problem, but what about the request? I need to be able to see what has been sent in my production environment by logging the XML to a DB-field.

like image 987
Marcus W Avatar asked May 16 '12 13:05

Marcus W


2 Answers

Right now there's no easy way to get the request XML. But as you noticed, Savon logs each request and response to a specified logger. So if you're not changing the log level, you could use a custom Logger-like object that responds to :debug and store what gets logged.

module DBLogger
  def self.debug(message)
    p message  # replace with custom impl.
  end
end

Savon.configure do |config|
  config.logger = DBLogger
end

Hope that helps!

like image 172
rubiii Avatar answered Nov 19 '22 15:11

rubiii


This wasn't explicitly stated in the official answer, but I had to include a couple more options to get requests/responses to log properly. In my case, I just used Rails.logger instead of rolling my own:

client = Savon.client(
  wsdl: 'http://example.com?wsdl',
  log: true,
  logger: Rails.logger,
  log_level: :debug
)

client.call(:example_operation, message: {example: 'example'})

The log_level might default to debug, but there's no harm in being explicit :) But without the log: true option, you won't see the actual request/response bodies, only urls and statuses.

I'm using Savon 2.7.2 for reference.

like image 27
Jaime Bellmyer Avatar answered Nov 19 '22 13:11

Jaime Bellmyer