Marcus W Marcus W - 1 year ago 186
Ruby Question

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')

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.

Answer Source

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.

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

Hope that helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download