I am sending a SOAP POST and I am getting a "HTTPError: HTTP Error 415: Unsupported Media Type" @ 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)
What am I doing wrong? Thanks!
The
Content-Length
value in theheaders
dictionary seems wrongand also some other values.
I would fix it with:
I know that this is fixed, but I spent quite a while with the same error and with no workable solution and wanted to get this solution out there in case someone else was having the same issue that I was. After several hours of searching I noticed that the file I was looking at was using SOAP v1.2. This is potentially a problem because Suds (to my knowledge) doesn't yet support v1.2.
I found a workaround to make Suds think that it is using v1.2 here: SOAP 1.2 python client. I'm sure this won't work for everyone, as this 415 error can be caused by many different things, but it worked for me and there are very few solutions to this problem so the more we can get here the better. I've pasted the code that worked for me below (there were a few potential solutions on that page).
In the headers you have Content-Type listed twice.
The message you are sending is using the SOAP 1.1 namespace which would match the second Content-Type (text/xml). Based on the error I would guess the first Content-Type (application/soap+xml), which is for SOAP 1.2 message is actually being sent to the server. Remove the first Content-Type, that should fix if your server is truly expecting a SOAP 1.1 message.