我一直在使用 soap4r 并尝试使用 SOAP::Header::SimpleHandler,我试图让它在传出消息上放置自定义 header ,但我不知道如何获取它包括属性而不是作为子元素:
class ServiceContext < SOAP::Header::SimpleHandler
NAMESPACE = "http://context.core.datamodel.fs.documentum.emc.com/"
def initialize()
super(XSD::QName.new(NAMESPACE, 'ServiceContext'))
XSD::QName.new(nil, "Identities")
end
def on_simple_outbound
username = "username"
password = "password"
docbase = "Test"
return {"Identities" => {"Username" => username, "Password" => password, "Docbase" => docbase}}
end
end
返回:
<n1:ServiceContext xmlns:n1="http://context.core.datamodel.fs.documentum.emc.com/"
env:mustUnderstand="0">
<n1:Identities>
<n1:Username>username</n1:Username>
<n1:Password>password</n1:Password>
<n1:Docbase>Test</n1:Docbase>
</n1:Identities>
</n1:ServiceContext>
我需要它返回的是以下内容:
<ServiceContext xmlns="http://context.core.datamodel.fs.documentum.emc.com/">
<Identities xsi:type="RepositoryIdentity" userName="_USER_" password="_PWD_" repositoryName="_DOCBASE_" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</ServiceContext>
非常感谢任何帮助。
最佳答案
soap4r 不是很漂亮。我仔细查看了 rdocs abit,看起来解决问题的最简单方法是让 on_simple_outbound
返回您要创建的元素的字符串表示形式。
所以而不是
return {"Identities" => {"Username" => username, "Password" => password, "Docbase" => docbase}}
尝试
%Q(<Identities xsi:type="RepositoryIdentity" userName="#{user}" password="#{password}" repositoryName="#{docbase}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>)
使用像 builder 这样的东西,你可以让它看起来更 ruby 色,但试试吧。
另一种选择是研究较新的 soap 库。 handsoap看起来很有趣。
关于ruby - soap4r 自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/630050/