Replicating XML Request with Savon/Ruby

送分小仙女□ 提交于 2019-12-20 04:36:55

问题


I am trying to avoid using Nokogiri/Builder to build my XML and would like to instead use the Savon gem with Ruby 2.0.0. I have the following request I need to replicate:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <GetList xmlns="http://tempuri.org/">
      <listRequest xmlns:a="http://schemas.datacontract.org/2004/07/Services.List"
             i:type="b:NpsListRequest"
             xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:b="http://schemas.datacontract.org/2004/07/Services.List.Strategies">
        <a:id>1</a:id>         
      </listRequest>
    </GetList>
  </s:Body>
</s:Envelope>

So far I have this:

  def soap_client
    soap_client = Savon.client(
        wsdl: "http://10.10.10.10/ListApi.svc?wsdl"
        headers: {"Authorization" =>  "Basic"},
        basic_auth: ['username', 'password'],
        env_namespace: :s,
        ssl_verify_mode: :none,
        log: true,
        :pretty_print_xml => true
    )
  end

Then soap_client.call :get_list, message: {'id' => 1} which returns this:

<s:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:tns="http://tempuri.org/"
            xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <tns:GetList>
      <id>1</id>
    </tns:GetList>
  </s:Body>
</s:Envelope>

I can't figure out how to replicate the first request exactly. the tns: namespace on GetList is wrong, and I can't replicate the <listRequest xmlns:a = piece either. Any thoughts on how to do this within Savon?


回答1:


The namespace on GetList is correct. What you probably need to write is

soap_client.call(:get_list,
                 :attributes => {'xmlns:b'=>'http://schemas.datacontract.org/'},
                 message: { 'ListRequest' => { 'tns:id' => 1 } }

That won't be the exact solution for your problem, because I don't have access to your wsdl and can't test. But you should get the key to a solution.



来源:https://stackoverflow.com/questions/21714271/replicating-xml-request-with-savon-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!