首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何组织这个SOAP请求包中的语法,在一个红宝石脚本中,这个包是与gem Savon一起发送的?

如何组织这个SOAP请求包中的语法,在一个红宝石脚本中,这个包是与gem Savon一起发送的?
EN

Stack Overflow用户
提问于 2014-05-01 20:57:54
回答 1查看 384关注 0票数 0

我试图将SOAP请求发送到web上的资源。它预计将收到以下信息:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <DiscoverParameterValues xmlns="http://comscore.com/">
      <parameterId>loc</parameterId>
      <query xmlns="http://comscore.com/ReportQuery">
        <Parameter KeyId="geo" Value="124" />
      </query>
    </DiscoverParameterValues>
  </soap:Body>
</soap:Envelope>

这是我用来发送请求的Ruby代码,它将产生类似于上面的XML。

代码语言:javascript
复制
require 'savon'
#Connect to the Comscore API
client = Savon.client(
            wsdl:"https://api.comscore.com/KeyMeasures.asmx?WSDL", 
            basic_auth: ["username", "pw" ],
            log: true, :pretty_print_xml=>true)

这是生成的XML:

代码语言:javascript
复制
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:tns="http://comscore.com/"
              xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
              xmlns:ins0="http://comscore.com/Report"
              xmlns:ins1="http://comscore.com/ReportQuery"
              xmlns:ins2="http://comscore.com/ReportModel"
              xmlns:ins3="http://comscore.com/FetchMedia"
              xmlns:ins4="http://comscore.com/Media/Response">
  <env:Body>
    <tns:DiscoverParameterValues>
      <tns:parameterId>loc</tns:parameterId>
   ==>   <tns:query>
        <tns:Parameter>
          <tns:attributes>
            <tns:KeyId>geo</tns:KeyId>
            <tns:Value>124</tns:Value>
          </tns:attributes>
        </tns:Parameter>
    ==>  </tns:query>
    </tns:DiscoverParameterValues>
  </env:Body>
</env:Envelope>

如您所见,我的代码中的XML与所需的XML的外观有所不同。我用"=>“标记了它

下面是我用来构造查询的gem文档页面:

http://savonrb.com/version2/locals.html

我试过玩这个游戏,但我不明白如何才能让这些“属性”正常工作。

有什么建议吗?

更新:

在阅读文章之后,我将ruby语法更改为:

代码语言:javascript
复制
   attributes = { "KeyId"=>"geo", "Value" =>"124"}
   parameter = {
                :parameterId=>"loc",
                :query=>{ "Parameter" => { attributes: attributes}}
               }

response = client.call(:discover_parameter_values, 
                message:{:parameterId=>"loc", 
                        :query =>{ "Parameter"=> "", :attributes! => 
                                 { "Parameter" => 
                                     { "KeyId" => "geo" , "Value"=>"124" }}},
                     },
                :attributes => {"xmlns" => "http://comscore.com/" })

我的XML更改为:

代码语言:javascript
复制
    <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:tns="http://comscore.com/"
                  xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:ins0="http://comscore.com/Report"
                  xmlns:ins1="http://comscore.com/ReportQuery"
                  xmlns:ins2="http://comscore.com/ReportModel"
                  xmlns:ins3="http://comscore.com/FetchMedia"
                  xmlns:ins4="http://comscore.com/Media/Response">
  <env:Body>
    <tns:DiscoverParameterValues xmlns="http://comscore.com/">
      <tns:parameterId>loc</tns:parameterId>
      <tns:query>
        <tns:Parameter KeyId="geo" Value="124"/>
      </tns:query>
    </tns:DiscoverParameterValues>
  </env:Body>
</env:Envelope>

因此,除了-我不能在“查询”标记上设置“属性”之外,一切都可以工作。

我想将它设置为查询的一个属性:"xmlns=http://comscore.com/ReportQuery

我该怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-01 21:25:54

问:它有效吗?

Savon以与第一个示例不同的方式使用名称空间qualifiert。在下面的片段中,其中的标记具有属性xmlns中指定的隐式命名空间。

代码语言:javascript
复制
<DiscoverParameterValues xmlns="http://comscore.com/">
  <parameterId>loc</parameterId>
  <query xmlns="http://comscore.com/ReportQuery">
    <Parameter KeyId="geo" Value="124" />
  </query>
</DiscoverParameterValues>

Savon使用了不同的语法。它在信封中定义了所有需要的命名空间,并在后面使用每个标记的前缀。相同表达式的不同语法。SoapUI经常使用前者的变体,而Savon则使用后者。

ParameterId在两种情况下都来自命名空间http://comscore.com/

最新答复:

你可以走不同的路:

通常是骗人的:我只写完全合格的钥匙。

代码语言:javascript
复制
...
:query => { "Parameter" => "", :attributes! =>
           { "Parameter" =>
              { "ins1:KeyId" => "geo", "ins1:Value" => "124" }}},
...

如果您绝对希望将键作为属性放在"query“上,那么您可以这样定义它:

代码语言:javascript
复制
response = client.call(:discover_parameter_values, 
            message:{:parameterId=>"loc", 
                    :query =>{ "Parameter"=> "", :attributes! => 
                             { "Parameter" => 
                                 { "KeyId" => "geo" , "Value"=>"124" }}},
                    :attributes! => {'tns:query' =>
                           { "xmlns" => "http://comscore.com/ReportQuery" }
                 },
            :attributes => {"xmlns" => "http://comscore.com/" })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23416491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档