[英]Savon - SOAP - ruby - 400 Bad Request
我正在尝试使用 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 中执行此操作不是有效的解决方案。
我想知道是否有人能看到我所缺少的东西。 空的<SOAP-ENV:Header />
标签是问题所在吗?如果是,我如何将其添加到 Savon 请求中?
谢谢,斯图尔特
在我的例子中,我有重复的命名空间,所以我得到了 400 Bad Request。
我的代码:
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而不是调用并打印请求正文:
client.build_request(:search, message: {...})
puts request.body
我将请求正文粘贴到SoapUI
中,然后我进行了一项一项的更改,直到请求成功。
这里的问题是我的 http 标头:因为 url 中有空格,所以必须对 url 进行编码。 但是,我必须在将它传递给 Savon 之前对其进行编码,然后 Savon 再次对其进行编码 - 这个双重编码的 url 失败了。 请参阅: https ://stackoverflow.com/questions/14482251/ruby-savon-soap-request-double-escapes-spaces-in-url
谢谢,斯图尔特
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.