开发者

Soap Custom XMl in Ruby with Savon

I need the request body to look as such:

<env:Body>

<abc:request token="0" id="1" version="4">
<category domain_id="630643"/>
</abc>

</env:Body>

How do I add the additional attributes to ?

Additionally, how do you change the encoding from UTF-8 to UTF-16? 开发者_如何转开发Not able to find this in the documentation.


SOAP body with custom attributes:

client.request :abc, :request, :token => 0, :id => 1, :version => 4 do
  soap.body = {
    :category => "",
    :attributes! => { :category  => { "domain_id" => 630643 } }
  }
end

Savon::Client#request accepts a namespace, the name of the SOAP action to call as well as an optional Hash of attributes for the SOAP input tag.

<abc:request version="4" id="1" token="0">

Attributes for SOAP body tags can be set via a special :attributes! Hash. Notice that the domain_id attribute is a String, because Hash key Symbols are (by default) converted to lowerCamelCase.

<category domain_id="630643"></category>

Also notice, that Gyoku did not create a self-closing tag. Savon uses Gyoku to translate Ruby Hashes to XML and the library is able to create self-closing tags, but it seems to swallow custom attributes for those (v0.4.2). This is a bug and should be fixed with the next release.

Further information and examples can be found in the new documentation.


Changing the default encoding

Savon uses HTTPI to execute HTTP requests and you can access the HTTPI::Request object via Savon::Client#http. Changing the default encoding to UTF-16 should be possible by setting a custom "Content-Type" header.

client.http.headers["Content-Type"] = "text/xml;charset=UTF-16"

Please note, that this doesn't change the encoding attribute of the xml processing instruction:

<?xml version="1.0" encoding="UTF-8"?>

Take a look at "Send UTF-16 encoded SOAP request with Ruby and Savon" for information about how to change the processing instruction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜