How to create SOAP nested headers with SAVON - ruby

I would like to generate following soap header with SAVON but cannot really find any proper documentation for it any help would be appreciated.
<SOAP-ENV:Header>
<trp:protocolVersion>4.0</trp:protocolVersion>
<trp:id>*******</trp:id>
<trp:userId>*******</trp:userId>
<trp:service crv:objectType=“SERVICE”>
<crv:memberClass>xxxxxx</crv:memberClass>
<crv:memberCode>xxxxxx</crv:memberCode>
<crv:subsystemCode>xxxxxxxx</crv:subsystemCode>
<crv:serviceCode>xxx</crv:serviceCode>
</trp:service>
</SOAP-ENV:Header>
This is what I have so far, but I am struggling to build the nested objectType in the header
{
"trp:protocolVersion" => 4.0
"trp:id" => id,
"trp:userId" => user_id
"trp:service" => {
'',
:attributes!=> {
'trp:service' => {'objectType' =>'SERVICE'}} },
}

Related

Adding a property to xml request

I'm using Savon Ruby gem to call a SOAP API, I have the request almost ready but one thing is bugging still.
savonclient = Savon.client(wsdl: 'test.wsdl',
endpoint: 'http://endpoint.biz/',
log: true,
log_level: :debug,
pretty_print_xml: true,
convert_request_keys_to: :none,
namespaces: { 'xmlns:buggy' => 'http://otherdomain.biz/something.xsd'},
soap_header: { 'buggy:client' => {
"id:property1" => 'value1',
"id:property2" => 'value2'
}
This produces
<env:Header>
<buggy:client>
<id:property1>value1</id:property1>
<id:propert2>value2</id:property2>
</buggy:client>
What I need is to have property after buggy:client, like this:
<buggy:client id:objecttype="something">
I tried adding attributes hash for the soap_header, but it only creates a new attribute below .
Any ideas?

Objects with xsi:type in Savon 2

I'm trying to create the following soap envelope but I can't add a type and VALUE at same time in some object
<env:Envelope>
<env:Body>
<typens:SendRequest>
<typens:someStuff>123</typens:someStuff>
<typens:someAuthStuff xsi:type="somenamespace:SomeObject">VALUE</typens:someAuthStuff>
</typens:SendRequest>
</env:Body>
</env:Envelope>
This is my message:
client.call(:send, message: {
someStuff: 123,
someAuthStuff: { '#xsi:type' => 'somenamespace:SomeObject' },
}
)
But...How can I add the 'VALUE' in someAuthStuff?
Very similar question as How to use objects with xsi:types in Savon but now in Savon 2.x

Complex soap_header xml objects for ruby savon gem

Working on a project with this WSDL: http://developer.ebay.com/webservices/latest/ebaysvc.wsdl
$client = Savon.client(
:wsdl => "http://developer.ebay.com/webservices/latest/ebaysvc.wsdl",
:endpoint => "https://api.sandbox.ebay.com/wsapi?callname=AddItem&siteid=0&version=733& Routing=new",
:headers => { "Content-Type" => "application/soap+xml", "SOAPAction" => "AddItemRequest"},
:namespace => "urn:ebay:apis:eBLBaseComponents",
:soap_header => { CREDENTIALS GO HERE: SEE BELOW }
)
What I need is a complex type xml format. Where the first part of soap_header is RequestCredentials and has several child nodes, and one of those (Credentials) has 3 child nodes... I also need to escape the lower camel case and just use camel case.
The final result with the xml output should look something like:
<urn:RequestCredentials>
<urn:eBayAuthToken>
AUTH TOKEN HERE
</urn:eBayAuthToken>
<urn:Credentials>
<urn:DevId>DEVID HERE</urn:DevId>
ect..... (two more child nodes (AppId, and AuthCert)
</urn:Credentials>
</urn:RequestCredentials>
Here is the error I receive:
14007
Error
SOAP Authentication failed due to missing or invalid security header.
Any ideas on how to correctly format the xml with the savon gem?

Constructing this SOAP header properly?

I'm attempting to build a proper SOAP header:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sen="https://webservices.averittexpress.com/SendWebImageService">
<soapenv:Header
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ns:authnHeader
soapenv:mustUnderstand="0"
xmlns:ns="http://webservices.averittexpress.com/authn">
<Username>xxxxxxxx</Username> <Password>xxxxxxxx</Password>
</ns:authnHeader> </soapenv:Header>
This is my Savon client call, I'm using version 2:
client = Savon.client(
wsdl: api_url,
raise_errors: false,
convert_request_keys_to: :camelcase,
element_form_default: :qualified,
env_namespace: :soapenv,
soap_header: {'username' => username, 'password' => password }
)
I'm getting the following SOAP error:
fault=>
{:faultcode=>"soapenv:Server",
:faultstring=>"java.lang.NullPointerException",
:detail=>nil}}
How do I get the sapoenv:mustUserstand="0" line, and what is it?
Plus I'm confused how to set the xmlns:ns="http://webservices.averittexpress.com/authn">.
I've little experience using SOAP requests, and am coming from a Ruby/Rails RESTful background. Any help would be appreciated.
Savon uses Gyoku to convert both SOAP header and body to XML.
Following this libraries conventions, here's what your Hash needs to look like:
soap_header = {
"ns:authnHeader" => {
"#soapenv:mustUnderstand" => 0,
"#xmlns:ns" => "http://webservices.averittexpress.com/authn",
"Username" => "x",
"Password" => "x"
}
}
Savon.client(:soap_header => soap_header)

Parse WSDL file with SOAP4R

Is there any example of WSDL Parser using SOAP4R? I'm trying to list all operations of WSDL file but I can't figure it out :( Can you post me some tutorial?
Thx
Maybe that isn't answer you want, but I recommend you switch to Savon. For example, your task looks like this snippet (this example taken from github's savon page):
require "savon"
# create a client for your SOAP service
client = Savon::Client.new("http://service.example.com?wsdl")
client.wsdl.soap_actions
# => [:create_user, :get_user, :get_all_users]
# execute a SOAP request to call the "getUser" action
response = client.request(:get_user) do
soap.body = { :id => 1 }
end
response.body
# => { :get_user_response => { :first_name => "The", :last_name => "Hoff" } }

Resources