我试图用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请求?
谢谢,Stuart
问题出在我的http头。因为url里面有空格,所以url必须被编码。然而,我必须在传递给Savon之前对它进行编码,然后Savon再对它进行编码--这个双重编码的url失败了。请看 https:/stackoverflow.comquestions14482251ruby-savon-soap-request-double-escapes-spaces-in-url。
谢谢,Stuart
我得到的是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选项,错误就消失了。
我是如何发现这个问题的?
使用 构建请求 而不是 召唤 并打印请求主体。
client.build_request(:search, message: {...})
puts request.body
把你从调试中得到的请求粘贴到SoapUI中 然后逐一修改,直到请求在SoapUI中成功。