如何使用Savon生成以下XML

时间:2014-09-30 04:34:13

标签: ruby web-services wsdl savon

我一直在努力知道如何通过Savon构建一个具有不同命名空间的不同标记的SOAP XML请求 - 如下所示 - 但尚未能够实现。有人可以帮帮我吗?下面写的请求是由SOAPUI生成的(将其链接到WSDL)。注意名称空间的urn和urn1前缀。我使用Savon 2,Ruby 1.9和Rails 3。

<soapenv:Envelope xmlns:urn="urn:safetypay:messages:mws:api"       xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn1="urn:safetypay:schema:mws:api">
   <soapenv:Body>
     <urn:ExpressTokenRequest>
        <urn1:Language>ES</urn1:Language>
        <urn:Signature>2984764ac8214cca0a44e06badaba4bbc93ed381af59199f65d9c</urn:Signature>
     </urn:ExpressTokenRequest>
   </soapenv:Body>
</soapenv:Envelope>

提前谢谢你:)

我想分享解决方案:

最后,我能够解开它。请注意,为了安全和/或简洁目的,本论坛中发布的代码并不完全是真实的(它不完整)。

# Instance of the Savon client using the respective wsdl. Notice the two (multiple) namespaces.
# Also, see the namespace prefix urn (making it different to the default one). 
# In addition,the code changed the envelope namespace prefix to soapenv.
# While debugging in console (irb), I used the parameters log: true, pretty_print_xml: true inside the following client instance too (e.g. ...env_namespace: :soapenv, log: true, pretty_print_xml: true... ). This allowed me to see very easily the XML Savon was sending when requesting.
# Remember to require the necessary resources. Maybe require 'savon', require 'digest',...

client = Savon.client(wsdl: "/MerchantExpressWs.wsdl", namespaces: {"xmlns:urn" => "urn:safetypay:messages:mws:api",   "xmlns:urn1"  =>"urn:safetypay:schema:mws:api"}, namespace_identifier: :urn, env_namespace: :soapenv)

# Assignment of variables below must be done in this case. I don't write it here to be this code not so large.

# A thing I lasted much time to know how to do was to add the prefix urn1 only to the tag Language (urn1:Language), since the code was adding urn to all due to the namespace_identifier: :urn (see the client instance above). As you can see, I simply added it to the array message (message1).

message1 = {"urn1:Language" => language, "Signature"=> signature  }

# The respective operation for this request is create_express_token.

response = client.call(:create_express_token, message: message1 )

# This was the way Savon sent the request as needed by the Web Service. Savon added additional namespaces in comparison to the original XML request I wanted to send, but it was not problematic, it was OK (e.g., it added some additional stuff to the namespaces soapenv, urn, urn1)

我希望这篇文章可以帮助许多人通过Savon-Ruby(也许还有Rails)轻松使用SOAP Web服务。

主要感谢上帝为每一件好事:)

0 个答案:

没有答案