2

I am using savon 2 gem. How to add the xmlns to message tag instead of envelope?

The xml generate is this

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.serviceurl.com/"> <soap:Body> <tns:Search> <Token>55</Token> </tns:Search> </soap:Body> </soap:Envelope>

I need this

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Search xmlns="http://www.serviceurl.com/"> <Token>55</Token> </Search> </soap:Body> </soap:Envelope>

i.e Search xmlns="http://www.serviceurl.com/"

Steffen Roller
  • 3,464
  • 25
  • 43
pra
  • 343
  • 1
  • 3
  • 16

3 Answers3

2

I have a very simple solution for that. Just use a string as the tag and specify the namespace. You missed sharing your code, so I can only guess what it might look like, but you should change it to something like this:

'tns:Token' => 'SS'
Steffen Roller
  • 3,464
  • 25
  • 43
1

In XML terms the difference between these two requests is not the Search element, but rather that in the "current" version the Token element is not in a namespace, whereas in the "need" version the Token is in the http://www.serviceurl.com/ namespace.

You should be able to achieve the effect you require by setting the client's element_form_default parameter to :qualified

Savon.client(element_form_default: :qualified)

(docs)

This will probably produce

<tns:Search>
  <tns:Token>

but as far as an XML parser is concerned that is equivalent to the version with a default namespace declaration (and if the service doesn't accept this form then the bug lies in the service, not in your client).

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Yes the search and token are in tns namespace. But the service i am calling does not recognize the token. – pra May 07 '14 at 09:33
  • Then you need to complain to the service provider and get them to fix their broken service... If all else fails you can always construct the XML by hand (the "xml" option on [this page](http://savonrb.com/version2/locals.html)) but that means you have to handle any necessary escaping of illegal characters within the `Token` value. – Ian Roberts May 07 '14 at 10:26
1

Try like this:

client=Savon.client(wdsl: ...)
client.call(:search, attributes: {xmlns: "http://www.serviceurl.com/"}) do 
  message ({ 
    "Token" => 55
  })
end