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 - 在 Rails 中将 Devise 身份验证助手与 `OR` 语句相结合

ruby-on-rails - has_many 通过一个额外的列

c# - 帖子 : OnMethodBoundaryAspect and WebMethod

iphone - 如何将文件上传到webservice

java - Eclipse 中的 tomcat : Deploy more than one project

ruby-on-rails - 如何将一个变量分配给另一个变量而不互相引用?

ruby-on-rails - 具有特定保存 ID 的 Rails 5 left_outer_join

ruby - 如何限制使用 Sequel 和 Ruby 查询返回的字符数?

arrays - 任意数量数组的交集

ruby - 如何在 Ruby 中创建 CSV 文件的某些列的副本,其中一列中包含不同的数据?