萨翁+ Rails 2 如何修改XML的请求结构

发布于 2024-11-16 20:34:07 字数 1990 浏览 2 评论 0原文

我正在使用 Savon 开发 Web 服务客户端。由于我是初学者,所以我决定首先尝试使用示例 WDSL,在我的例子中是:

http://www.webservicex.com/CurrencyConvertor.asmx?wsdl< /p>

我的控制器非常简单:

require 'savon'

class WebServiceController < ApplicationController  
  def index
    puts "web_service: IN"    
    client = Savon::Client.new do
      wsdl.document = "http://www.webservicex.com/CurrencyConvertor.asmx?wsdl"
    end

    response = client.request :conversion_rate do
      soap.body = {
        :from_currency => 'USD',
        :to_currency => 'EUR'
      }
    end    
    puts response.to_hash;    
    render :text => response.to_hash    
  end
end

该代码生成的 XML 是:

<env:Envelope   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns:wsdl="http://www.webserviceX.NET/" 
                xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
        <ConversionRate>
            <wsdl:fromCurrency>USD</wsdl:fromCurrency>
            <wsdl:toCurrency>EUR</wsdl:toCurrency>
        </ConversionRate>
    </env:Body>
</env:Envelope>

但是,XML 应该是(我知道这一点,因为我正在使用soapUI):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                    xmlns:web="http://www.webserviceX.NET/">
<soapenv:Header/>
   <soapenv:Body>
      <web:ConversionRate>
         <web:FromCurrency>USD</web:FromCurrency>
         <web:ToCurrency>EUR</web:ToCurrency>
      </web:ConversionRate>
   </soapenv:Body>
</soapenv:Envelope>

我知道我的 XML 请求不起作用,因为我总是得到“0”(零)作为响应,并且生成了“正确的”XML 请求通过soapUI,我得到了正确的值(例如“0.6959”...)。

我的代码中缺少什么吗?

谢谢你!!!

I'm using Savon to develop a Web Service Client. Since I'm a beginner I decided to try at first with an example WDSL, which in my case is:

http://www.webservicex.com/CurrencyConvertor.asmx?wsdl

My controller is very simple:

require 'savon'

class WebServiceController < ApplicationController  
  def index
    puts "web_service: IN"    
    client = Savon::Client.new do
      wsdl.document = "http://www.webservicex.com/CurrencyConvertor.asmx?wsdl"
    end

    response = client.request :conversion_rate do
      soap.body = {
        :from_currency => 'USD',
        :to_currency => 'EUR'
      }
    end    
    puts response.to_hash;    
    render :text => response.to_hash    
  end
end

The XML produced by that code is:

<env:Envelope   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns:wsdl="http://www.webserviceX.NET/" 
                xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
        <ConversionRate>
            <wsdl:fromCurrency>USD</wsdl:fromCurrency>
            <wsdl:toCurrency>EUR</wsdl:toCurrency>
        </ConversionRate>
    </env:Body>
</env:Envelope>

However, the XML should be (and I know this because I'm using soapUI):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                    xmlns:web="http://www.webserviceX.NET/">
<soapenv:Header/>
   <soapenv:Body>
      <web:ConversionRate>
         <web:FromCurrency>USD</web:FromCurrency>
         <web:ToCurrency>EUR</web:ToCurrency>
      </web:ConversionRate>
   </soapenv:Body>
</soapenv:Envelope>

I know my XML Request isn't working because I always get '0' (zero) as response, and with the "right" XML Request generated by soapUI I get correct values (such as '0.6959' ...).

Is something missing in my code?

Thank you!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雄赳赳气昂昂 2024-11-23 20:34:07

有两件事:

  1. 您需要将 :wsdl 添加到
  2. 您需要确保标签的拼写正确的调用中,

更改为

response = client.request :wsdl, :conversion_rate do

和 ,

"FromCurrency" => 'USD',
"ToCurrency" => 'EUR'

这应该可以为您完成。

two things:

  1. you need to add :wsdl to the call
  2. you need to make sure the spelling of the tags is correct

change to

response = client.request :wsdl, :conversion_rate do

and to

"FromCurrency" => 'USD',
"ToCurrency" => 'EUR'

that should do it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文