37

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

7 Answers 7

52

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
1
  • (replace to_s with pretty above) Jan 24, 2018 at 19:00
51

You can directly via the Savon::Client#build_request method.

Example:

request = client.build_request(:some_operation, some_payload)
request.body # Get the request body
request.headers # Get the request headers

Take a peak @ https://github.com/savonrb/savon/blob/master/lib/savon/request.rb for the full doc.

1
  • This should be the correct answer as it's the most direct and uses Savon without having to modify any parts of it. May 22, 2017 at 18:51
13

While I'm sure there's a better way to do this, I just overrode response.

class Savon::SOAP::Request
  def response
    pp   self.request.headers
    puts
    puts self.request.body
    exit
  end
end
2
  • 1
    This was the cheap and easy approach for helping debug. Thanks.
    – Jim Garvin
    Nov 3, 2011 at 13:32
  • and what about if I wanted to this but also send the request to server?
    – Fakada
    Jul 1, 2013 at 12:20
13

I am using Savon 2.11 and I can accomplish it with globals in the client:

def client
  @client ||= Savon.client(soap_version: 2,
                           wsdl:         config.wsdl,
                           logger:       Rails.logger,
                           log:          true)
end

More info on the globals here.

Then the logger spits out the host, the http verb and the complete xml ("headers" and body) for both request and response.

1
10

They've updated the API since the last post. Set this setting in Savon.client: :pretty_print_xml => true. After your call, search the logs for SOAP request:. The output is put to stdout. Check the console console history if you're testing your connection from the console.

8

Savon uses HTTPI to execute SOAP requests. HTTPI is a common interface on top of various Ruby HTTP clients. You could probably mock/stub the HTTP request executed by Savon via:

HTTPI.expects(:post).with do |http|
  SchemaValidation.validate(:get_user, http.body)
end

Please note that I used Mocha for mocking the SOAP request, getting the HTTP body and validating it against some validation method (pseudo-code).

Currently, Savon does not support building up requests without executing them. So the only way to validate the request would be to intercept it.

If you would need Savon to support this feature, please let me know and open a ticket over at GitHub.

EDIT: There's also savon_spec, which is a little helper for basic fixture-based testing with Savon.

1
  • Thanks for the suggestion. I can live without the feature, just wanted to know if it was possible. May 3, 2011 at 14:55
7

I had the same issue and patched Savon as follows:

module Savon
  class Client
    def get_request_xml operation_name, locals
      Savon::Builder.new(operation_name, @wsdl, @globals, locals).pretty
    end
  end
end

This builds the XML and returns it as a string without sending it to the API endpoint. It doesn't accept a block argument in the same way client.call does, so it won't be able to reproduce every type of request you're making, but it meets my needs for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.