How can I populate header values in SOAP document?

H

Hartin, Brian

Hi all,

Got a question about the Ruby SOAP library. I've generated a proxy
using the SOAP::WSDLDriverFactory as follows:

ws =3D
SOAP::WSDLDriverFactory.new('http://foo.com/foo.wsdl').create_rpc_driver

I can see that this generates methods for each defined operation, and I
use them thusly:

ws.WSGetResults:)maxRecords =3D> 1, ...)

The method and parameters are translated to the appropriate XML in the
SOAP message body. However, some operations require a 'User' and
'Password' to be in the message _header_. I've looked through the
documentation and mailing lists with no luck. How can one achieve this?

My code is listed below.

Thanks!

Brian Hartin

---- soaptest.rb ----
require 'soap/wsdlDriver'
require 'date'

ws =3D
SOAP::WSDLDriverFactory.new('http://foo.com/foo.wsdl').create_rpc_driver
ws.generate_explicit_type =3D true
r =3D ws.WSHeartbeat({}) # Works because it doesn't require authenticatio=
n
r =3D g.WSGetXmlResults:)recordId =3D> 123, :recordLimit =3D> 200) # Does=
n't
work

---- Example SOAP request, according to vendor ----
<?xml version=3D"1.0" encoding=3D"utf-8"?>
<soap:Envelope xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd=3D"http://www.w3.org/2001/XMLSchema"
xmlns:soap=3D"http://schemas.xmlsoap.org/soap/envelope/">
=20 <soap:Header>
=20 <!-- I don't know how to populate the header -->
=20 <WSAuthHeader xmlns=3D"http://foo.com/WSFooSvc">
=20 <User>string</User>
=20 <Password>string</Password>
=20 </WSAuthHeader>
=20 </soap:Header>
=20 <soap:Body>
=20 <WSGetXmlResults xmlns=3D"http://foo.com/WSFooSvc">
=20 <recordId>int</recordId>
=20 <recordLimit>int</recordLimit>
=20 </WSGetXmlResults>
=20 </soap:Body>
</soap:Envelope>
*************************************************************************=
***=20
This email may contain confidential material.=20
If you were not an intended recipient,=20
Please notify the sender and delete all copies.=20
We may monitor email to and from our network.=20
*************************************************************************=
***
 
J

Jeremy Hinegardner

I'm not sure if it can be done dynamically, but you can create a
ClientSimpleHeader which hooks into the inbound and outbound stream.
I've only hooked into the outbound and it looked something like the code
below.

The code is untested and extrapolated from my case to yours, so take the
general idea and see where it gets you.

And if you didn't know, driver.wiredump_dev is your friend.

Good Luck.

enjoy,

-jeremy

----- customer_header.rb -----
require 'soap/header/simplehandler'
require 'stringio'

class CustomSimpleHeader < SOAP::Header::SimpleHandler
# encapsulate outbound only headers, this is set once and never
# touched again.
#
def initialize(itemname,namespace,childdata)
super(XSD::QName.new(namespace,name))

case childdate
# other classes if you want
# Or do something with XML::Mappging
when Hash
xml = StringIO.new
childdata.each_pair do |key,value|
xml.puts("<#{key}>#{value}</#{key}>")
end
@item = xml.string
else
@item = childdata.to_s
end
end

def on_simple_outbound
@item if @item
end

def on_simple_inbound
end
end

---- possible soaptest.rb -----
require 'customer_header'

# assuming this works with a dynamic proxy

ws = SOAP::WSDLDriverFactor.new("http://foo.com/foo.wsdl").create_rpc_driver
ws.wiredump_dev = STDERR
ws.generate_explicit_type = true
user = "my user"
password = "my password"
ws.headerhandler << CustomSimpleHeader.new("WSAuthHeader",
{ "User" => user,
"Password" => password })
 
A

ara.t.howard

On Fri, 27 Oct 2006, Jeremy Hinegardner wrote:

<snip code help>

jeremy: it's nice to see you're not programming python for a living anymore!

;-)

-a
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top