HTTP Error 415 What am I doing wrong?

I send a SOAP POST and get an 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? Thank!

+3
source share
3 answers

The meaning Content-Lengthin the dictionary headersseems wrong

'Content-Length: len(data)' 

as well as some other meanings.

I would fix this 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'
}
+5
source

, , , - , . , , , SOAP v1.2. , Suds ( ) v1.2.

, Suds , v1.2 : SOAP 1.2 python. , , 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 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()
+4

Content-Type.

The message you send uses the SOAP 1.1 namespace, which will correspond to the second Content-Type (text / xml). Based on the error, I would suggest that the first Content-Type (application / soap + xml), which for the SOAP 1.2 message is actually sent to the server. Remove the first Content-Type that should fix if your server is really expecting SOAP 1.1 messages.

+1
source

Source: https://habr.com/ru/post/1796689/


All Articles