我正在发送SOAP POST,我收到“HTTPError:HTTP错误415:不支持的媒体类型”@ response = urllib2.urlopen(req)
data = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 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">
<soap:Header>
<AutotaskIntegrations xmlns="http://autotask.net/ATWS/v1_5/">
<PartnerID>1</PartnerID>
</AutotaskIntegrations>
</soap:Header>
<soap:Body>
<getThresholdAndUsageInfo xmlns="http://autotask.net/ATWS/v1_5/">
</getThresholdAndUsageInfo>
</soap:Body>
</soap:Envelope>"""
headers = {
'Content-Type': 'application/soap+xml; charset=utf-8'
'Host: "webservices.autotask.net"'
'Content-Type: text/xml; charset=utf-8'
'Content-Length: len(data)'
'SOAPAction: "http://autotask.net/ATWS/v1_5/getThresholdAndUsageInfo"'
}
site = 'https://webservices.autotask.net/atservices/1.5/atws.asmx'
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='webservices.autotask.net',
uri=site,
user='george.lastname@domain.com',
passwd='mypw')
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
page = urllib2.urlopen(site) #errors out 415 here
req = urllib2.Request(site, data, headers)
response = urllib2.urlopen(req)
我做错了什么?谢谢!
答案 0 :(得分:5)
Content-Length
字典中的headers
值似乎错误
'Content-Length: len(data)'
以及其他一些值。
我会解决它:
headers = {
'Content-Type': 'application/soap+xml; charset=utf-8',
'Host': 'webservices.autotask.net',
'Content-Length': len(data),
'SOAPAction': 'http://autotask.net/ATWS/v1_5/getThresholdAndUsageInfo'
}
答案 1 :(得分:4)
我知道这是固定的,但我花了相同的错误并没有可行的解决方案花了很长时间,并希望得到这个解决方案,以防其他人遇到与我相同的问题。经过几个小时的搜索,我注意到我正在查看的文件是使用SOAP v1.2。这可能是一个问题,因为Suds(据我所知)还不支持v1.2。
我找到了一种解决方法,让Suds认为它在这里使用v1.2:SOAP 1.2 python client。我确信这对每个人都不起作用,因为这个415错误可能是由许多不同的事情引起的,但它对我有用,而且这个问题的解决方案很少,所以我们可以得到更多更好。我已粘贴下面适合我的代码(该页面上有一些可能的解决方案)。
from suds.client import Client
from suds.bindings import binding
import logging
USERNAME = 'username'
PASSWORD = 'password'
# Just for debugging purposes.
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
# Telnic's SOAP server expects a SOAP 1.2 envelope, not a SOAP 1.1 envelope
# and will complain if this hack isn't done.
binding.envns = ('SOAP-ENV', 'http://www.w3.org/2003/05/soap-envelope')
client = Client('client.wsdl',
username=USERNAME,
password=PASSWORD,
headers={'Content-Type': 'application/soap+xml'})
# This will now work just fine.
client.service.someRandomMethod()
答案 2 :(得分:1)
在标题中,您列出了两次Content-Type。
您发送的消息使用的SOAP 1.1命名空间与第二个Content-Type(text / xml)匹配。根据错误,我猜第一个Content-Type(application / soap + xml),它实际上是用于发送到服务器的SOAP 1.2消息。删除第一个Content-Type,如果您的服务器确实需要SOAP 1.1消息,则应该修复该内容类型。