Constructing this SOAP header properly? - ruby

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)

Related

invalid SOAP request needs experienced eye

TLDR: the solution can be found here
I'm using savon to make requests against a SOAP service. I know... Gross.
Regardless, I'm having trouble making Savon behave. The SOAP provider has this validator, which takes the following inputs:
Web Service: ProductData
Version: 1.0.0
Operation: getProductSellable
Endpoint: https://psproductdata100-stg.pcna.online
When I use the validator, I enter this xml:
<GetProductSellableRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/">
<wsVersion xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/">1.0.0</wsVersion>
</GetProductSellableRequest>
And I get this response body
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetProductSellableResponse xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/">
<ErrorMessage xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/">
<code>110</code>
<description>Authentication Credentials Required</description>
</ErrorMessage>
</GetProductSellableResponse>
</s:Body>
</s:Envelope>
That response is valid because I did not provide my un/pw. If I do provide credentials, I get a full response. Below is a screenshot of that happening in my browser.
However, when I use Savon to make the same request
#!/usr/bin/env ruby
require 'savon'
require 'awesome_print'
require 'byebug'
require 'pry'
endpoint = 'https://psproductdata100-stg.pcna.online'
path = 'psProductData.svc?singleWsdl'
wsdl = "#{endpoint}/#{path}"
args = {
wsdl: wsdl,
log: true,
log_level: :debug,
pretty_print_xml: true,
element_form_default: :qualified
}
client = Savon.client(args) do
convert_request_keys_to :lower_camelcase
end
message = { ws_version: '1.0.0' }
response = client.call(:get_product_sellable) do
message(message)
end
ap response
The response does not come back as expected. The XML looks close to what was sent by the validator, but not exact.
Heres the request
D, [2018-04-26T18:01:00.471662 #89854] DEBUG -- : HTTPI /peer GET request to psproductdata100-stg.pcna.online (net_http)
I, [2018-04-26T18:01:00.979809 #89854] INFO -- : SOAP request: https://psproductdata100-stg.pcna.online/psProductData.svc
I, [2018-04-26T18:01:00.979886 #89854] INFO -- : SOAPAction: "getProductSellable", Content-Type: text/xml;charset=UTF-8, Content-Length: 501
D, [2018-04-26T18:01:00.980107 #89854] DEBUG -- : <?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:tns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/">
<env:Body>
<tns:GetProductSellableRequest>
<tns:wsVersion>1.0.0</tns:wsVersion>
</tns:GetProductSellableRequest>
</env:Body>
</env:Envelope>
And the response
D, [2018-04-26T18:01:00.980224 #89854] DEBUG -- : HTTPI /peer POST request to psproductdata100-stg.pcna.online (net_http)
I, [2018-04-26T18:01:01.650449 #89854] INFO -- : SOAP response (status 200)
D, [2018-04-26T18:01:01.650731 #89854] DEBUG -- : <?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetProductSellableResponse xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/">
<ErrorMessage xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/">
<code>110</code>
<description>Version mismatch.</description>
</ErrorMessage>
</GetProductSellableResponse>
</s:Body>
</s:Envelope>
and the output from Savon
{
:get_product_sellable_response => {
:error_message => {
:code => "110",
:description => "Version mismatch.",
:#xmlns => "http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/"
},
:#xmlns => "http://www.promostandards.org/WSDL/ProductDataService/1.0.0/"
},
:"#xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
:"#xmlns:xsd" => "http://www.w3.org/2001/XMLSchema"
}
Thanks to the other answers on this thread, I found this post. I read up on how namespaces work, as well as linked XSD files. By combining my new knowledge with that post, I was a able to build out something like this:
require 'savon'
require 'awesome_print'
require 'byebug'
require 'pry'
endpoint = 'https://psproductdata100-stg.pcna.online'
path = 'psProductData.svc?singleWsdl'
wsdl = "#{endpoint}/#{path}"
args = {
wsdl: wsdl,
log: false,
log_level: :debug,
pretty_print_xml: true,
element_form_default: :qualified
}
client = Savon.client(args) do
convert_request_keys_to :lower_camelcase
namespaces 'xmlns:shar' => 'http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/'
end
message = {
'shar:wsVersion' => '1.0.0',
'shar:id' => 'REDACTED',
'shar:password' => 'REDACTED'
}
response = client.call(:get_product_sellable) do
message(message)
end
ap response.body[:get_product_sellable_response][:product_sellable_array][:product_sellable][0..2]
Now i'm more fun to talk to at dinner parties.
Similar problem with namespaces ruby savon and wsdl namespacing. I believe you have the same problem.
Additional Note: Try to use SOAP UI to debug, the generated XML from the WSDL and see the difference. Also change the XML to the XML generated from the code and validate the XML in SOAP UI. It will show the errors if any in SOAP UI, will make debugging faster and errors more understandable.
I believe your issue is a namespace problem. The wsVersion is within the shared object namespace "ins0". It is not in the "tns" namespace. Try changing your request to look like the following:
<env:Body>
<tns:GetProductSellableRequest>
<ins0:wsVersion>1.0.0</ins0:wsVersion>
</tns:GetProductSellableRequest>
</env:Body>

Savon/Ruby - Unable to add ':info' to specific XML tag

I am currently trying to run a Soap Call in Ruby. Using Savon client, I am not getting the response I want. This is the XML I want to create:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sch="http://test.com/requests/test-manager/schema">
<soapenv:Header/>
<soapenv:Body>
<sch:createTestDateRequest>
<sch:testMethod>DD</sch:testMethod>
<sch:testPeriodNumber>999</sch:testPeriodNumber>
<sch:testDateTime>2017-12-14 00:00:00</sch:testDateTime>
<sch:name>Automated test</sch:name>
</sch:createTestDateRequest>
</soapenv:Body>
</soapenv:Envelope>
And I've written the following code to create that Soap Call:
require 'savon'
$soap_client_testmgr = Savon.client(
wsdl: 'http://test.com/test-manager-ws/TestManagerSoapService?wsdl',
namespace: 'http://test.com/requests/test-manager/schema',
env_namespace: :soapenv,
namespace_identifier: :sch,
pretty_print_xml: true,
log: true,
log_level: :debug)
$soap_client_testmgr.call(:create_test_date, message: {
:testMethod => 'DD',
:testPeriodNumber => $test_period,
:testDateTime => (Date.today+7).strftime('%Y-%m-%d') + ' 00:00:00',
:name => 'Automated test'})
But the XML that is generated is the following, missing the :sch before every individual name tag. This should be taken from the WSDL, correct? I've looked into namespace identifiers, but I have yet to find a fitting solution.
<soapenv:Body>
<sch:createTestDateRequest>
<testMethod>DD</testMethod>
<testPeriodNumber>999</testPeriodNumber>
<testDateTime>2017-12-14 00:00:00</testDateTime>
<name>Automated test</name>
</sch:createTestDateRequest>
</soapenv:Body>
</soapenv:Envelope>
What could solve this? I just need to add the sch: before the name tag.
Thanks!
Fixed it by using string interpolation:
$soap_client_testmgr.call(:create_test_date, message: {
:"sch:testMethod" => 'DD',
:"sch:testPeriodNumber" => $test_period,
:"sch:testDateTime" => (Date.today+7).strftime('%Y-%m-%d') + ' 00:00:00',
:"sch:name" => 'Automated test'})

Passing Array Elements for Savon 2 (SOAP)

I'm trying to write code in Ruby with the Savon gem (v2) that fetches account information from a SOAP api, but I'm having an issue with passing an Array.
CampaignIds is supposed to be an array of integers.
Here is my code:
client = Savon.client(wsdl: "https://api7secure.publicaster.com/Pub7APIV1/Campaign.svc?singleWsdl")
message = {
"EncryptedAccountID" => api_key,
"APIPassword" => api_password,
"CampaignIds" => [3,4],
"StartDate" => yesterday,
"EndDate" => yesterday,
"IncludeTests" => false
}
client.call(:get_comparative_report_details_data, message: message)
which produces the following request:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:tns="http://BlueSkyFactory.Publicaster7.Public.API" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<tns:GetComparativeReportDetailsData>
<tns:EncryptedAccountID>*****</tns:EncryptedAccountID>
<tns:APIPassword>*****</tns:APIPassword>
<tns:CampaignIds>3</tns:CampaignIds>
<tns:CampaignIds>4</tns:CampaignIds>
<tns:StartDate>2014-01-06</tns:StartDate>
<tns:EndDate>2014-01-06</tns:EndDate>
<tns:IncludeTests>false</tns:IncludeTests>
</tns:GetComparativeReportDetailsData>
</env:Body>
</env:Envelope>
whereas, if I play around in SOUP UI, the request should look like:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:blu="http://BlueSkyFactory.Publicaster7.Public.API" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<soapenv:Header/>
<soapenv:Body>
<blu:GetComparativeReportData>
<blu:EncryptedAccountID>*****</blu:EncryptedAccountID>
<blu:APIPassword>*****</blu:APIPassword>
<blu:CampaignIds>
<arr:int>3</arr:int>
<arr:int>4</arr:int>
</blu:CampaignIds>
<blu:StartDate>2014-01-06T16:21:47-05:00</blu:StartDate>
<blu:EndDate>2014-01-07T16:21:47-05:00</blu:EndDate>
<blu:IncludeTests>false</blu:IncludeTests>
</blu:GetComparativeReportData>
</soapenv:Body>
</soapenv:Envelope>
Any Ideas?
You can try this syntax :
message = {
...
"CampaignIds" => {"int" => [3,4]},
...
}
That'll produce this output :
<CampaignIds>
<int>3</int>
<int>4</int>
</CampaignIds>
Hope this helps.
I just experienced the same issue and found a solution, that's not dependent on having a permissive SOAP endpoint. You can configure this behavior as a global using the unwrap key. This key is given as an option to Gyoku which generates the XML for Savon.
client = Savon.client(wsdl: 'https://example.com/wsdl', unwrap: true)
client.call(:cook_meal, message: { 'Ingredients' => ['tomato', 'basil', 'mozzarella'] })
Although the issue is old, better late than never.

Case problem in SOAP message tag names using savon

I'm using Ruby 1.9.2 with savon 0.9.2 on Windows 7 Professional 64 bit.
I need to call a web SOAP service that requires a security token that I get from a second web SOAP service. The code I use is as follows:
require 'savon'
client = Savon::Client.new "http://some.url?wsdl"
client.wsdl.soap_actions
start_session_response = client.request :start_session do
soap.input = ["StartSession", {:xmlns => "http://some.schema" } ]
soap.body = { :userName => "User", :password => "password" }
end
do_something_response = client.request :do_something do
soap.input = [ "DoSomething", { :xmlns => "http://some.schema"} ]
soap.body = { :securityToken => start_session_response.to_hash[:start_session_response][:security_token] }
end
This results in XML that looks like:
<?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://some.schema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<DoSomething xmlns="http://some.schema">
<wsdl:securityToken>
<wsdl:tokenType>sessiontoken</wsdl:tokenType>
<wsdl:token>
.
.
.
</wsdl:token>
</wsdl:securityToken>
</DoSomething>
</env:Body>
</env:Envelope>
Never mind the weird namespace convention (or is that just me) in this XML that is savon doing its thing.
The problem I face is that the tags inside the securitytoken tag all start with a lower case letter where they should be upper case. So <tokenType> and <token> should have been <TokenType> and <Token>.
In my opinion the definition of these tags are all in the WSDL that is used to create the savon client. That definition seems not to be used or used incorrectly.
What can I do to get the correct XML/SOAP message from savon?
For later releases of Savon, you should be able to supply a 'global' option of convert_request_keys_to when you initialize your Savon client:
# In Savon 2
Savon.client wsdl:"http://some.url?wsdl", convert_request_keys_to: :camelcase
According to comments in the source file, it accepts one of :lower_camelcase, :camelcase, :upcase, or :none.
I had a similar problem with Savon and ended up using strings in stead of symbols for my hash keys, you could try something like:
soap.body = { 'TokenType'=> 'some_value', 'Token' => 'some_value' }
Savon uses Gyoku for the conversion of tags I believe. To change the symbol conversion you can insert the following statement:
Gyoku.convert_symbols_to :camelcase # or one of [:none, :lover_camelcase]
hope that helps.

Talking with a SOAP service using Savon gem in Ruby

I'm trying to communicate with a soap service and I know that I should send a SOAP Envelope like this:
POST /webpay_test/SveaWebPay.asmx HTTP/1.1
Host: webservices.sveaekonomi.se
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://webservices.sveaekonomi.se/webpay/CreateOrder"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<CreateOrder xmlns="https://webservices.sveaekonomi.se/webpay">
<request>
<Order>
<ClientOrderNr>string</ClientOrderNr>
<CustomerReference>string</CustomerReference>
<OrderDate>dateTime</OrderDate>
<CountryCode>string</CountryCode>
<SecurityNumber>string</SecurityNumber>
<CustomerEmail>string</CustomerEmail>
<IsCompany>boolean</IsCompany>
<PreApprovedCustomerId>long</PreApprovedCustomerId>
<AddressSelector>string</AddressSelector>
</Order>
<InvoiceRows>
<ClientInvoiceRowInfo>
<ArticleNr>string</ArticleNr>
<Description>string</Description>
<PricePerUnit>double</PricePerUnit>
<NrOfUnits>double</NrOfUnits>
<Unit>string</Unit>
<VatPercent>int</VatPercent>
<DiscountPercent>int</DiscountPercent>
<ClientOrderRowNr>int</ClientOrderRowNr>
</ClientInvoiceRowInfo>
<ClientInvoiceRowInfo>
<ArticleNr>string</ArticleNr>
<Description>string</Description>
<PricePerUnit>double</PricePerUnit>
<NrOfUnits>double</NrOfUnits>
<Unit>string</Unit>
<VatPercent>int</VatPercent>
<DiscountPercent>int</DiscountPercent>
<ClientOrderRowNr>int</ClientOrderRowNr>
</ClientInvoiceRowInfo>
</InvoiceRows>
</request>
</CreateOrder>
</soap:Body>
</soap:Envelope>
here is the code I've wrote:
client = Savon::Client.new("https://webservices.sveaekonomi.se/webpay_test/SveaWebPay.asmx?wsdl")
res = client.create_order do |soap|
soap.namespace = "https://webservices.sveaekonomi.se/webpay_test/CreateOrder.asmx"
soap.body = { :auth => { :username => "username", :password => "pass", :client_number => "1111" },
:order => { :client_order_nr => "1000000", :customer_reference => "4212", :order_date => Date.today,
:country_code => "SE", :security_number => "1111111111", :is_company => false,
:customer_email => "me#gmail.com", :pre_approved_customer_id => 0 },
:invoice_rows => { :client_invoice_row_info => { :article_nr => "x100", :description => "something cool -- description",
:price_per_unit => 100, :nr_of_units => 3, :unit => "SEK", :vat_percent => 25,
:discount_percent => 0, :client_order_row_nr => "1"},
:client_invoice_row_info => { :article_nr => "x200", :description => "something cooler -- description",
:price_per_unit => 200, :nr_of_units => 2, :unit => "SEK", :vat_percent => 25,
:discount_percent => 0, :client_order_row_nr => "1" }
}
}
end
and it generates this, which is different from what I have as the template and that's why I'm getting an error:
<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:wsdl="https://webservices.sveaekonomi.se/webpay_test/CreateOrder.asmx" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<wsdl:CreateOrder>
<invoiceRows>
<clientInvoiceRowInfo>
<clientOrderRowNr>1</clientOrderRowNr>
<pricePerUnit>200</pricePerUnit>
<nrOfUnits>2</nrOfUnits>
<unit>SEK</unit>
<vatPercent>25</vatPercent>
<articleNr>x200</articleNr>
<discountPercent>0</discountPercent>
<description>something cooler -- description</description>
</clientInvoiceRowInfo>
</invoiceRows>
<order>
<customerEmail>me#gmail.com</customerEmail>
<preApprovedCustomerId>0</preApprovedCustomerId>
<countryCode>SE</countryCode>
<clientOrderNr>1000000</clientOrderNr>
<securityNumber>11111111</securityNumber>
<customerReference>4212</customerReference>
<isCompany>false</isCompany>
<orderDate>2010-06-28</orderDate>
</order>
<auth>
<password>pass</password>
<clientNumber>1111</clientNumber>
<username>username</username>
</auth>
</wsdl:CreateOrder>
</env:Body>
</env:Envelope>
and here is the response:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<CreateOrderResponse xmlns="https://webservices.sveaekonomi.se/webpay">
<CreateOrderResult>
<Accepted>false</Accepted>
<ErrorMessage>Failed to create or authorize order</ErrorMessage>
<SveaOrderNr>0</SveaOrderNr>
<RejectionCode>Error</RejectionCode>
<WillBuyInvoices xsi:nil="true" />
<AuthorizeId>0</AuthorizeId>
<AuthorizedAmount xsi:nil="true" />
<ExpirationDate xsi:nil="true" />
</CreateOrderResult>
</CreateOrderResponse>
</soap:Body>
</soap:Envelope>
could anyone tell me how can I solve this problem. and since I'm a newbie when it comes to SOAP would you also tell me if the order of the xml tags in the soap:Body tag is important or not?
Thanks to Steve, I found "Why is “wsdl” namespace interjected into action name when using savon for ruby soap communication?" where Nick and Steve were talking about a similar problem.
Like Nick, my problem was in the way Savon is cooking up a SOAP envelope. As recommended by Nick, I ended up monkey patching a couple of methods in the Savon SOAP class. It's in lib/savon/soap.rb
and I'm good to go now.
I'm a novice when it comes to SOAP and it's my first time writing a SOAP client, but honestly it SUCKS! I still remember my first time writing a client for a REST service and gosh it was fun.
REST ROCKS, SOAP SUCKS. that's all!
You are missing the <request> element.
Try replacing your soap.body with a single hash with a key of ::request and a value of the existing hash record that you have already.
EDIT 1:
Your namespace line within your code should be "https://webservices.sveaekonomi.se/webpay" not the full URL you have there currently.

Resources