代码之家  ›  专栏  ›  技术社区  ›  timsabat

无效的SOAP请求需要有经验的眼睛

  •  0
  • timsabat  · 技术社区  · 6 年前

    TLDR:可以找到解决方案 here

    我正在使用 savon 对SOAP服务发出请求。我知道。。。总的

    不管怎样,我很难让Savon规矩点。SOAP提供程序具有 this validator,它接受以下输入:

    Web Service: ProductData
    Version: 1.0.0
    Operation: getProductSellable
    Endpoint: https://psproductdata100-stg.pcna.online
    

    使用验证器时,输入以下xml:

    <GetProductSellableRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/">
      <wsVersion xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/">1.0.0</wsVersion>
    </GetProductSellableRequest>
    

    我得到了这个反应体

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Header />
      <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <GetProductSellableResponse xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/">
          <ErrorMessage xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/">
            <code>110</code>
            <description>Authentication Credentials Required</description>
          </ErrorMessage>
        </GetProductSellableResponse>
      </s:Body>
    </s:Envelope>
    

    这个回答是有效的,因为我没有提供我的un/pw。如果我提供了凭据,我会得到完整的响应。下面是我的浏览器中发生的情况的屏幕截图。

    image

    然而,当我使用Savon提出同样的请求时

    #!/usr/bin/env ruby
    
    require 'savon'
    require 'awesome_print'
    require 'byebug'
    require 'pry'
    
    endpoint = 'https://psproductdata100-stg.pcna.online'
    path = 'psProductData.svc?singleWsdl'
    wsdl = "#{endpoint}/#{path}"
    
    args = {
      wsdl: wsdl,
      log: true,
      log_level: :debug,
      pretty_print_xml: true,
      element_form_default: :qualified
    }
    
    client = Savon.client(args) do
      convert_request_keys_to :lower_camelcase
    end
    
    message = { ws_version: '1.0.0' }
    response = client.call(:get_product_sellable) do
      message(message)
    end
    
    ap response
    

    响应没有按预期返回。XML看起来 验证程序发送的内容,但不准确。

    这是请求

    D, [2018-04-26T18:01:00.471662 #89854] DEBUG -- : HTTPI /peer GET request to psproductdata100-stg.pcna.online (net_http)
    I, [2018-04-26T18:01:00.979809 #89854]  INFO -- : SOAP request: https://psproductdata100-stg.pcna.online/psProductData.svc
    I, [2018-04-26T18:01:00.979886 #89854]  INFO -- : SOAPAction: "getProductSellable", Content-Type: text/xml;charset=UTF-8, Content-Length: 501
    D, [2018-04-26T18:01:00.980107 #89854] DEBUG -- : <?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:tns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/">
      <env:Body>
        <tns:GetProductSellableRequest>
          <tns:wsVersion>1.0.0</tns:wsVersion>
        </tns:GetProductSellableRequest>
      </env:Body>
    </env:Envelope>
    

    以及回应

    D, [2018-04-26T18:01:00.980224 #89854] DEBUG -- : HTTPI /peer POST request to psproductdata100-stg.pcna.online (net_http)
    I, [2018-04-26T18:01:01.650449 #89854]  INFO -- : SOAP response (status 200)
    D, [2018-04-26T18:01:01.650731 #89854] DEBUG -- : <?xml version="1.0"?>
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <GetProductSellableResponse xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/">
          <ErrorMessage xmlns="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/">
            <code>110</code>
            <description>Version mismatch.</description>
          </ErrorMessage>
        </GetProductSellableResponse>
      </s:Body>
    </s:Envelope>
    

    以及Savon的输出

    {
        :get_product_sellable_response => {
            :error_message => {
                       :code => "110",
                :description => "Version mismatch.",
                     :@xmlns => "http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/"
            },
                   :@xmlns => "http://www.promostandards.org/WSDL/ProductDataService/1.0.0/"
        },
                         :"@xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
                         :"@xmlns:xsd" => "http://www.w3.org/2001/XMLSchema"
    }
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   timsabat    6 年前

    多亏了这条线上的其他答案,我发现 this 邮递我了解了名称空间的工作原理,以及链接的XSD文件。通过将我的新知识与那篇文章相结合,我能够构建如下内容:

    require 'savon'
    require 'awesome_print'
    require 'byebug'
    require 'pry'
    
    endpoint = 'https://psproductdata100-stg.pcna.online'
    path = 'psProductData.svc?singleWsdl'
    wsdl = "#{endpoint}/#{path}"
    
    args = {
      wsdl: wsdl,
      log: false,
      log_level: :debug,
      pretty_print_xml: true,
      element_form_default: :qualified
    }
    
    client = Savon.client(args) do
      convert_request_keys_to :lower_camelcase
      namespaces 'xmlns:shar' => 'http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/'
    end
    
    message =  {
      'shar:wsVersion' => '1.0.0',
      'shar:id' => 'REDACTED',
      'shar:password' => 'REDACTED'
    
    }
    response = client.call(:get_product_sellable) do
      message(message)
    end
    
    ap response.body[:get_product_sellable_response][:product_sellable_array][:product_sellable][0..2]
    

    现在在晚宴上和我聊天更有趣了。

        2
  •  0
  •   sashwat    6 年前

    命名空间存在类似问题 ruby savon and wsdl namespacing .我相信你也有同样的问题。

    附加说明:尝试使用SOAP UI调试从WSDL生成的XML,并查看差异。还要将XML更改为从代码生成的XML,并在SOAP UI中验证XML。它将显示SOAP UI中的错误(如果有),这将使调试更快,错误更容易理解。

        3
  •  0
  •   Paul Fleischman    6 年前

    我认为您的问题是名称空间问题。wsVersion位于共享对象命名空间“ins0”内。它不在“tns”命名空间中。尝试更改您的请求,使其如下所示:

    <env:Body>
      <tns:GetProductSellableRequest>
        <ins0:wsVersion>1.0.0</ins0:wsVersion>
      </tns:GetProductSellableRequest>
    </env:Body>
    
    推荐文章