Skip to content

Instantly share code, notes, and snippets.

@rubiii
Created January 29, 2012 12:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubiii/1698636 to your computer and use it in GitHub Desktop.
Save rubiii/1698636 to your computer and use it in GitHub Desktop.
Monkey patch Savon to allow attributes to be added to the SOAP header tag
require "savon"
module Savon
module SOAP
class XML
attr_accessor :header_attributes
def to_xml
@xml ||= tag(builder, :Envelope, complete_namespaces) do |xml|
# adds the header_attributes:
tag(xml, :Header, header_attributes) { xml << header_for_xml } unless header_for_xml.empty?
if input.nil?
tag(xml, :Body)
else
tag(xml, :Body) { xml.tag!(*add_namespace_to_input) { xml << body_to_xml } }
end
end
end
end
end
end
client = Savon::Client.new do
wsdl.endpoint = "http://example.com"
wsdl.namespace = "http://v1.example.com"
end
client.request :something do
soap.header = { :key => "value" }
soap.header_attributes = { "xmlns:wsa" => "http://www.w3.org/2005/08/addressing" }
end
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wsdl="http://v1.example.com"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<key>value</key>
</env:Header>
<env:Body>
<something></something>
</env:Body>
</env:Envelope>
@jamescarr
Copy link

Why can't we just have this as part of savon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment