View Savon Request XML without Sending to Server

Peter Brown picture Peter Brown · May 2, 2011 · Viewed 21k times · Source

I'm using the Savon gem to make a SOAP request using code similar to what's below. It's working, but I would like to view/capture the request XML without actually making a call to their server. I can view it now after a request is made by sticking a debugger line after the request and inspecting the client variable.

Does anyone know of a way to view the request XML without actually making a request? I want to be able to validate the XML against a schema using Cucumber or Rspec.

client = Savon::Client.new do |wsdl, http|
  wsdl.document = "http://fakesite.org/fake.asmx?wsdl"
end

client.request(:testpostdata, :xmlns => "http://fakesite.org/") do
  soap.header = { :cAuthentication => {"UserName" => "MyName", "Password" => "MyPassword" } }
  soap.body = { :xml_data => to_xml }
end

Answer

carbonr picture carbonr · Aug 2, 2014

Using Savon 2 I do it this way, write a method that return the request body from the client.

 client = Savon::Client.new(....)

this is not mentioned in the documentation

  def get_request
     # list of operations can be found using client.operations
     ops = client.operation(:action_name_here)

     # build the body of the xml inside the message here        
     ops.build(message: { id: 42, name: "Test User", age: 20 }).to_s
  end