Rails - Savon set multiple namespaces

前提是你 提交于 2019-12-19 17:33:22

问题


I'm using savon version 2 (with Ruby on Rails )to invoke a webservice and i need to inject some additional namespaces to my Envelope. Something like:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:add="http://schemas.xmlsoap.org/ws/2003/03/addressing" 
xmlns:newNamespace1="http://someURL.pt/Test1" 
xmlns:newNamespace2="http://someURL.pt/Test2" 
xmlns:newNamespace3="http://someURL.pt/Test3"

My current code is:

client = Savon.client do
        wsdl "https://someValidURL?wsdl"

        namespace "http://someURL.pt/Test1" 
        namespace "http://someURL.pt/Test2" 
        namespace "http://someURL.pt/Test3"
end

response = client.call( ...the webservice call... )

...but in my request the Savon only puts the last namespace

<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsns="http://someURL.pt/Test3" 
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

I didn't find any documentation about this on Savon Git project.

Does anyone have a workaround for this problem??

PS- I also check that one possible solution is to set all the xml request (the envelope) to request but ... well... is too much like a hack.

If this is not possible and there's other good gem to do this, please tell me =)


回答1:


I found that is not possible (for now) to set multiple namespaces on version 2 of Savon.

For now i migrate my application on Savon version 1 and it worked =)

begin
    client = Savon::Client.new do
        wsdl.document = "https://someURL?wsdl"
    end

  @response = client.request :service do
       soap.namespaces["xmlns:test1"]  = "http:/someURLtest1"
       soap.namespaces["xmlns:test2"]  = "http:/someURLtest2" 

       soap.body = { #... the message....
          :"test1:something" => {}, 
          :"test2:something1" => {}
                   }
   end


  rescue Savon::Error => error
     log error.to_s
  end

More information here and here.

This question will be solved on the next version on Savon 2 with this code:

namespaces(
   "xmlns:first" => "http://someURL.pt/Test1",
   "xmlns:two"   => "http://someURL.pt/Testweewqwqeewq"
)



回答2:


Since Savon 2.1.0, it's possible to be done by setting the namespaces key with a hash of namespace definitions:

Savon.client({
  ...
  namespaces: {
    'xmlns:first' => 'http://someURL.pt/Test1',
    'xmlns:second' => 'http://someURL.pt/Test2',
    'xmlns:nth' => 'http://someURL.pt/TestN'
  }
})


来源:https://stackoverflow.com/questions/14501269/rails-savon-set-multiple-namespaces

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