ruby-on-rails - Savon + Rails 2 如何修改XML的请求结构

标签 ruby-on-rails ruby web-services savon

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

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

我的 Controller 非常简单:

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”(零)作为响应,并且使用 soapUI 生成的“正确”XML 请求我得到正确的值(例如“0.6959”...) .

我的代码中是否缺少某些内容?

谢谢!!!

最佳答案

两件事:

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

改为

response = client.request :wsdl, :conversion_rate do

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

这应该为您完成。

关于ruby-on-rails - Savon + Rails 2 如何修改XML的请求结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6427413/

相关文章:

ruby-on-rails - 获取数组并转换为哈希 Ruby

ruby-on-rails - FileUtils 是否需要不可用的 Ruby 版本?

c# - freedb.org - 如何在没有光盘 ID 的情况下按艺术家/专辑查询

ruby-on-rails - 我什么时候/为什么要在方法上引发异常?

ruby - 获取 Bundler 项目使用的 gem 列表

javascript - 在javascript中,ajax调用Web服务获取“403禁止访问”错误?

c++ - 错误 : 'memcpy_s' was not declared in this scope in gSOAP

ruby-on-rails - Ruby On Rails Heroku 数据库 :migrate Aborted!

ruby-on-rails - rails gem Simple_form 安装不工作

ruby-on-rails - 异常消息 : no such file to load in rails application