# Howto add automatically a soap header in soap4r?

**URL:** <https://www.ruby-forum.com/t/howto-add-automatically-a-soap-header-in-soap4r/107513>\
**Category:** Ruby\
**Created:** [June 22, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/howto-add-automatically-a-soap-header-in-soap4r/107513 "2007-06-22T13:11:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henning\_Panke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c4cdca/32.png) [@Henning\_Panke](https://www.ruby-forum.com/u/Henning_Panke)\
**Post date:** [June 22, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/howto-add-automatically-a-soap-header-in-soap4r/107513/1 "2007-06-22T13:11:13Z")

</div>

Hi all,  
I must access a webservices that expect the SOAP action as a soap  
header. I want to add this soap header automatically but I dont know  
where to hook in.  
My actual solution is to create a header and remove it after consuming.  
The code looks like this:

require ‘SystemDriver.rb’  
require ‘soap/header/simplehandler’

# define the action soap header

class ActionHandler \< SOAP::Header::SimpleHandler  
HeaderName =  
XSD::QName.new(‘[http://schemas.xmlsoap.org/ws/2004/08/addressing/','Action](http://schemas.xmlsoap.org/ws/2004/08/addressing/','Action)’)  
@m\_action

def initialize(action)  
super(HeaderName)  
@m\_action = action  
end

def on\_simple\_outbound  
@m\_action  
end  
end

# create the connection / interface

endpoint\_url = “[http://localhost:1500](http://localhost:1500)”  
obj = Public.new( endpoint\_url )

# enable debug output

obj.wiredump\_dev = STDERR if $DEBUG

ns = ‘[http://mynamespace.com/my/path](http://mynamespace.com/my/path)’

# getIdentity

# create action soap header

actionHandler = ActionHandler.new( ns + ‘/getIdentity’ )

# add action soap header

obj.headerhandler.add( actionHandler )

# do the request

emptyRequest = nil  
puts 'getIdentity: ’ + obj.getIdentity(emptyRequest).identityString  
puts ‘’

# remove action soap header for reusing the connection / interface

obj.headerhandler.delete( actionHandler )
