Ruby:Savon SOAP 请求收到 400 和 415 错误

发布于 2024-12-14 03:23:34 字数 3342 浏览 0 评论 0原文

我正在尝试使用 ruby​​ 库 Savon 发出 SOAP 请求。

我正在使用以下代码:

require "savon"

Savon.configure do |config|
    config.soap_version = 2  # use SOAP 1.2
    config.raise_errors = false
end

wsdl_logon = Savon::Client.new do
    wsdl.document = "https://api.affili.net/V2.0/Logon.svc?wsdl"
end

username = 'XXX'
password = 'YYY'

wsdl_logon.http.headers["Content-Type"] = "text/xml; charset=utf-8"

response = wsdl_logon.request "Logon" do
  soap.body = {'Username' => username, 'Password' => password, 'WebServiceType' => 'Product'}
end

if response.http_error?
    puts "Http Error!"
    puts y response.http_error
else
    puts "No Http Error!"
end

但我不断收到 400 错误消息(“错误请求”)。或者,如果删除以下行,

wsdl_logon.http.headers["Content-Type"] = "text/xml; charset=utf-8"

我将收到 415 错误消息(“不支持的媒体类型”)。

到目前为止,我一直在使用 PHP 发出这些请求,并且以下代码始终可以正常工作:

$soap_logon = new SoapClient('https://api.affili.net/V2.0/Logon.svc?wsdl');
$token = $soap_logon->Logon(array(
    'Username'      => 'XXX',
    'Password'      => 'YYY',
    'WebServiceType'    => 'Product'
));

任何人都可以为我指出正确的方向,可能的错误源可能是什么?我现在完全迷失了。

感谢您的帮助。


我按照 Tom De Leu 的建议进行操作,并尝试尽可能多地消除所生成的相关 SOAP 请求中的差异。但我仍然不断收到 400 错误。任何有关可能原因的暗示都将受到高度赞赏。

这是 PHP 生成的(工作)请求(为了清楚起见,在 XML 中添加了换行符):

POST /V2.0/Logon.svc HTTP/1.1
Host: api.affili.net
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.0-8+etch16
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://affilinet.framework.webservices/Svc/ServiceContract1/Logon"
Content-Length: 456

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://affilinet.framework.webservices/types"
    xmlns:ns2="http://affilinet.framework.webservices/Svc"
>
    <SOAP-ENV:Body>
        <ns2:LogonRequestMsg>
            <ns1:Username>xxx</ns1:Username>
            <ns1:Password>yyy</ns1:Password>
            <ns1:WebServiceType>Product</ns1:WebServiceType>
        </ns2:LogonRequestMsg>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是 Ruby 生成的(不工作)请求(为了清楚起见,添加了 xml 换行符)

SOAP request: https://api.affili.net/V2.0/Logon.svc
Content-Type: text/xml; charset=utf-8, SOAPAction: http://affilinet.framework.webservices/Svc/ServiceContract1/Logon, Content-Length: 605

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:wsdl="http://affilinet.framework.webservices/Svc"
    SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://affilinet.framework.webservices/types"
    xmlns:ns2="http://affilinet.framework.webservices/Svc"
>
    <SOAP-ENV:Body>
        <ns2:LogonRequestMsg>
            <ns1:Username>XXX</ns1:Username>
            <ns1:Password>YYY</ns1:Password>
            <wsdl:WebServiceType>Product</wsdl:WebServiceType>
        </ns2:LogonRequestMsg>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


HTTPI executes HTTP POST using the httpclient adapter
SOAP response (status 400):

I am trying to make a SOAP request using the ruby library Savon.

I am using the following code:

require "savon"

Savon.configure do |config|
    config.soap_version = 2  # use SOAP 1.2
    config.raise_errors = false
end

wsdl_logon = Savon::Client.new do
    wsdl.document = "https://api.affili.net/V2.0/Logon.svc?wsdl"
end

username = 'XXX'
password = 'YYY'

wsdl_logon.http.headers["Content-Type"] = "text/xml; charset=utf-8"

response = wsdl_logon.request "Logon" do
  soap.body = {'Username' => username, 'Password' => password, 'WebServiceType' => 'Product'}
end

if response.http_error?
    puts "Http Error!"
    puts y response.http_error
else
    puts "No Http Error!"
end

But I keep receiving 400 error messages ("bad request"). Or, if I remove the following line

wsdl_logon.http.headers["Content-Type"] = "text/xml; charset=utf-8"

I am receiving 415 error messages ("unsupported media type").

I have been using PHP to make these requests until now, and the following code always worked without problems:

$soap_logon = new SoapClient('https://api.affili.net/V2.0/Logon.svc?wsdl');
$token = $soap_logon->Logon(array(
    'Username'      => 'XXX',
    'Password'      => 'YYY',
    'WebServiceType'    => 'Product'
));

Can anybody point me to the right direction what a possible error source might be? I am completely lost right now.

Thank you for your help.


I did as Tom De Leu suggested, and tried to remove as many differences in the generated SOAP requests in question as possible. But I still keep receiving 400 errors. Any hint on possible reasons for this would be highly appreciated.

This is the (working) Request generated by PHP (linebreaks in XML added for clarity):

POST /V2.0/Logon.svc HTTP/1.1
Host: api.affili.net
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.0-8+etch16
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://affilinet.framework.webservices/Svc/ServiceContract1/Logon"
Content-Length: 456

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://affilinet.framework.webservices/types"
    xmlns:ns2="http://affilinet.framework.webservices/Svc"
>
    <SOAP-ENV:Body>
        <ns2:LogonRequestMsg>
            <ns1:Username>xxx</ns1:Username>
            <ns1:Password>yyy</ns1:Password>
            <ns1:WebServiceType>Product</ns1:WebServiceType>
        </ns2:LogonRequestMsg>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This is the (not working) request generated by Ruby (again, xml linebreaks added for clarity)

SOAP request: https://api.affili.net/V2.0/Logon.svc
Content-Type: text/xml; charset=utf-8, SOAPAction: http://affilinet.framework.webservices/Svc/ServiceContract1/Logon, Content-Length: 605

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:wsdl="http://affilinet.framework.webservices/Svc"
    SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://affilinet.framework.webservices/types"
    xmlns:ns2="http://affilinet.framework.webservices/Svc"
>
    <SOAP-ENV:Body>
        <ns2:LogonRequestMsg>
            <ns1:Username>XXX</ns1:Username>
            <ns1:Password>YYY</ns1:Password>
            <wsdl:WebServiceType>Product</wsdl:WebServiceType>
        </ns2:LogonRequestMsg>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


HTTPI executes HTTP POST using the httpclient adapter
SOAP response (status 400):

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-12-21 03:23:34

我发现我需要添加标头才能克服 415 错误。

Savon.client(wsdl: "www.sample_doman.com/endpoint?wsdl", headers: {'Content-Type' => 'application/soap+xml; charset=utf-8'})

I found that I needed to add the headers in to get past the 415 error.

Savon.client(wsdl: "www.sample_doman.com/endpoint?wsdl", headers: {'Content-Type' => 'application/soap+xml; charset=utf-8'})

予囚 2024-12-21 03:23:34

我建议查看 PHP 代码发送的 XML,然后将其与 Ruby Savon 代码发送的 XML 进行比较,并检查差异在哪里。然后看看是否可以修改 ruby​​ 代码来生成正确的请求。

I would suggest looking at the XML sent by the PHP code, then comparing it with the XML sent by the Ruby Savon code, and check where the differences are. Then see whether you can modify your ruby code to generate the correct request.

苄①跕圉湢 2024-12-21 03:23:34

告诉 Savon 使用 SOAP 版本 2(实际上是 1.2),然后手动将内容类型设置为 text/xml 有点达不到目的。

如果您的 Web 服务需要 SOAP 1.2,那么它需要“application/soap+xml”内容类型,如果您将soap_version 设置为 2,SAVON 将为您执行此操作。

如果您想要 text/xml 内容类型,只需将你的soap_version配置变量设置为1

Telling Savon to use SOAP version 2 (really 1.2) and then manually setting the content type to text/xml kind of defeats the purpose.

If your web service requires SOAP 1.2, then it is expecting a content type of 'application/soap+xml', which SAVON will do for you if you set the soap_version to 2.

If you want a content type of text/xml, just set your soap_version config variable to 1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文