Savon:向 body 标签添加编码

发布于 2024-12-17 04:11:38 字数 1765 浏览 0 评论 0原文

如何使用 Savon 将编码属性添加到 body 标记?

一些背景: 我正在尝试使用 savon 连接到 SOAP 资源。我可以获取 WSDL 文件并浏览这些方法。

@client = Savon::Client.new("http://some.domain.com/v2messaging/service?WSDL")

当我尝试使用登录方法时

response = @client.request :service, :login do
  soap.body = { 
    "String_1" => "username",
    "String_2" => "password"
  }
end

,出现此错误:

失败/错误:响应 = @client.request :service, :login do Savon::SOAP::Fault: (env:Client) 在处理请求时捕获异常:意外的编码风格:expected=http://schemas.xmlsoap.org/soap/encoding/,actual

body标签的差异。以下是预期的 xml(通过 SOAPUI 应用程序找到):

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:service="etapestryAPI/service">
   <env:header/>
   <env:body>
      <service:login env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <String_1>username</String_1>
        <String_2>password</String_2>
      </service:login>
   </env:body>
</env:Envelope>

Savon 发送:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:service="etapestryAPI/service" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://java.sun.com/jax-rpc-ri/internal" xmlns:ins1="etapestryAPI/service">
    <env:Body>
        <service:login>
            <String_1>username</String_1>
            <String_2>password</String_2>
        </service:login>
    </env:Body>
</env:Envelope>

这些之间有一些区别,但返回的错误与 env:login 标记上的 env:encodingStyle 属性有关。怎么添加这个属性呢?

How can I add an encoding attribute to the body tag using Savon?

Some background:
I am trying to use savon to connect to a SOAP resource. I can get the WSDL file and browse through the methods.

@client = Savon::Client.new("http://some.domain.com/v2messaging/service?WSDL")

when I try to use the login method

response = @client.request :service, :login do
  soap.body = { 
    "String_1" => "username",
    "String_2" => "password"
  }
end

I get this error:

Failure/Error: response = @client.request :service, :login do Savon::SOAP::Fault: (env:Client) caught exception while handling request: unexpected encoding style: expected=http://schemas.xmlsoap.org/soap/encoding/, actual

The difference in the body tag. Here is the expected xml (found through SOAPUI application):

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:service="etapestryAPI/service">
   <env:header/>
   <env:body>
      <service:login env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <String_1>username</String_1>
        <String_2>password</String_2>
      </service:login>
   </env:body>
</env:Envelope>

Savon sends:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:service="etapestryAPI/service" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://java.sun.com/jax-rpc-ri/internal" xmlns:ins1="etapestryAPI/service">
    <env:Body>
        <service:login>
            <String_1>username</String_1>
            <String_2>password</String_2>
        </service:login>
    </env:Body>
</env:Envelope>

There are a few difference between these, but the error returned has to do with the env:encodingStyle attribute on the env:login tag. How can add this attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝有风,海豚有海 2024-12-24 04:11:38

我想出了这个。要向函数标记添加属性(在本例中为登录),您可以向该方法传递一个附加参数:

response = @client.request :service, :login, "env:encodingStyle" => "http://schemas.xmlsoap.org/soap/encoding/" do
  soap.body = { 
           "String_1" => "username",
           "String_2" => "password"
         }
end

现在这可能无需传递块即可工作。

I figured this one out. To add an attribute to the function tag (in this case login), you can pass in an additional parameter to the method:

response = @client.request :service, :login, "env:encodingStyle" => "http://schemas.xmlsoap.org/soap/encoding/" do
  soap.body = { 
           "String_1" => "username",
           "String_2" => "password"
         }
end

This will probably now work without passing the block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文