【问题标题】:Savon - SOAP - ruby - 400 Bad RequestSavon - SOAP - ruby​​ - 400 错误请求
【发布时间】:2012-12-02 21:54:37
【问题描述】:

我正在尝试使用 Savon 通过 Ruby 发出 SOAP 请求,但我收到了来自服务器的 400 Bad Request 响应。

这是我试图提出的请求(根据soapUI):

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:apis="http://www.csisoftwareusa.com/ApiService">    
    <soap:Header/>    
    <soap:Body>
      <apis:AuthenticateConsumer>
         <!--Optional:-->
         <apis:consumerName>?</apis:consumerName>
         <!--Optional:-->
         <apis:consumerPassword>?</apis:consumerPassword>
      </apis:AuthenticateConsumer>    
    </soap:Body> 
</soap:Envelope>

这是我对 Ruby 提出的要求;它返回 400 Bad Request 错误:

<SOAP-ENV:Envelope>
  <SOAP-ENV:Body>
    <ins0:AuthenticateConsumer>
      <ins0:consumerName>?</ins0:consumerName>
      <ins0:consumerPassword>?</ins0:consumerPassword>
    </ins0:AuthenticateConsumer>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Http Headers: SOAPAction: "http://www.csisoftwareusa.com/ApiService/AuthenticateConsumer", Content-Type: text/xml;charset=UTF-8, Content-Length: 504

这是我能够用 Python 提出的请求。此请求成功:

<SOAP-ENV:Envelope>
  <SOAP-ENV:Header/>
  <ns0:Body>
    <ns1:AuthenticateConsumer>
      <ns1:consumerName>?</ns1:consumerName>
      <ns1:consumerPassword>?</ns1:consumerPassword>
    </ns1:AuthenticateConsumer>
  </ns0:Body>
</SOAP-ENV:Envelope>
Http headers: {'SOAPAction': u'"http://www.csisoftwareusa.com/ApiService/AuthenticateConsumer"', 'Content-Type': 'text/xml; charset=utf-8'}

我需要将对该 API 的调用集成到 Rails 应用程序中,因此在 Python 中执行此操作不是一个有效的解决方案。

我想知道是否有人能看到我缺少的东西。空的&lt;SOAP-ENV:Header /&gt; 标记是否是问题所在,如果是,我如何将其添加到 Savon 请求中?

谢谢, 斯图尔特

【问题讨论】:

  • 你的 ruby​​ 示例中的 &lt;/SOAP-ENV:Envelope&gt;&gt; 是什么?好像你有无效的xml。你真的应该使用像 Nokogiri 这样的 xml 构建器来构建它
  • @MichaelPapile 显然这是我对 StackOverflow 问题的格式的拼写错误;我重新运行了查询,最后一个括号不存在。我已经编辑了问题的文本来解决这个问题。谢谢!
  • 这方面有什么更新吗?我正在为同样的事情而苦苦挣扎。使用 Savon Gem 的第 2 版
  • @YeomansLeo 原来我的问题出在 http 标头中;我专门为此发布了一个新问题。 stackoverflow.com/questions/14482251/…

标签: ruby soap savon


【解决方案1】:

这里的问题在于我的 http 标头:因为 url 中有空格,所以必须对 url 进行编码。但是,我必须在将其传递给 Savon 之前对其进行编码,然后 Savon 再次对其进行编码 - 这个双重编码的 url 失败。见:https://stackoverflow.com/questions/14482251/ruby-savon-soap-request-double-escapes-spaces-in-url

谢谢, 斯图尔特

【讨论】:

    【解决方案2】:

    我收到了 HTTP 400,因为我的请求有重复的命名空间。

    我的代码:

    require 'savon'
    
    namespaces = {
      "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",
    }
    
    client = Savon.client(
     ...
     :namespace => namespaces
    }
    

    我删除了 :namespace 选项,错误消失了。

    我是怎么发现的?

    使用 build_request 而不是 call 并打印请求正文:

    client.build_request(:search, message: {...})
    puts request.body
    

    将调试中得到的请求粘贴到SoapUI中,并一一修改,直到SoapUI中的请求成功。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 2019-06-13
      相关资源
      最近更新 更多