Rails:使用Savon请求SOAP:无法处理请求

时间:2018-01-25 10:06:44

标签: ruby-on-rails savon

出于学习目的,我正在尝试使用gem Savon从GlobalWeather SOAP Web服务获取信息。 http://www.webservicex.net/globalweather.asmx?WSDL

我能够连接到API并检索操作:

[:get_weather, :get_cities_by_country]

但是当我尝试通过消息传递调用时,我得到关于未传递参数的错误:

Savon::SOAPFault in CitiesController#index
(soap:Server) System.Web.Services.Protocols.SoapException: Server was unable 
to process request. ---> System.Data.SqlClient.SqlException: Procedure or 
function 'getWCity' expects parameter '@CountryName', which was not 
supplied. at WebServicex.GlobalWeather.GetCitiesByCountry(String 
CountryName) --- End of inner exception stack trace ---

我的控制器城市代码如下:

class CitiesController < ApplicationController
  def index
    @operations = client.operations
    @response = client.call(:get_cities_by_country,  message: { CountryName:"Spain" })
  end

  def client
    Savon.client(wsdl: 'http://www.webservicex.net/globalweather.asmx?WSDL')
  end
end

我认为问题可能与根据文档将哈希消息转换为camelCase有关,所以我尝试使用全局选项convert_request_keys_to:none但没有发生任何不同。

class CitiesController < ApplicationController
  def index
    @operations = client.operations
    client = client do
      convert_request_keys_to :none 
    end
    @response = client.call(:get_cities_by_country,  message: { CountryName: "Spain" })
  end

  def client
    Savon.client(wsdl: 'http://www.webservicex.net/globalweather.asmx?WSDL')
  end
end

1 个答案:

答案 0 :(得分:0)

我想出来了,它必须确实使用convert_request_keys_to选项,但它应该在Savon :: client调用中传递

def client
  Savon.client(wsdl: 'http://www.webservicex.net/globalweather.asmx?WSDL', convert_request_keys_to: :none)
end