HTTP error 415 what am I doing wrong?

2019-05-10 07:23发布

问题:

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!

回答1:

The Content-Length value in the headers dictionary seems wrong

'Content-Length: len(data)' 

and also some other values.

I would fix it with:

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'
}


回答2:

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).

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()


回答3:

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.