How to add http headers in suds 0.3.6?
Asked Answered
H

2

6

I have an application in python 2.5 which sends data through suds 0.3.6.

The problem is that the data contains non-ascii characters, so I need the following header to exist in the soap message:

Content-Type="text/html; charset="utf-8"

and the header that exists in the SOAP message is just:

Content-Type="text/html"

I know that it is fixed in suds 0.4, but it requires Python2.6 and I NEED Python2.5 because I use CentOS and it needs that version. So the question is:

How could I change or add new HTTP headers to a SOAP message?

Headman answered 10/5, 2010 at 15:43 Comment(0)
H
4

When you create the opener in urllib2, you can use some handlers to do whatever you want. For example, if you want to add a new header in suds, you should do something like this:

https = suds.transport.https.HttpTransport()
opener = urllib2.build_opener(HTTPSudsPreprocessor)
https.urlopener = opener
suds.client.Client(URL, transport = https)

where HTTPSudsPreprocessor is your own handler, and it should look like this:

class HTTPSudsPreprocessor(urllib2.BaseHandler):

    def http_request(self, req):
        req.add_header('Content-Type', 'text/xml; charset=utf-8')
        return req

    https_request = http_request

The methods you have to override depend on what you want to do. See urllib2 documentation in Python.org

Headman answered 19/5, 2010 at 9:32 Comment(0)
A
13

At least in suds 0.4 (maybe earlier?) HTTP headers can also be passed in to the constructor or via the set_options method:

client = suds.client.Client(url, headers={'key': 'value'})
client.set_options(headers={'key2': 'value'})
Anecdotic answered 28/9, 2011 at 20:57 Comment(2)
This works and it's probably better than defining a new handler just to add headersEdward
I encountered a strnage error 405 Method not allowedFolacin
H
4

When you create the opener in urllib2, you can use some handlers to do whatever you want. For example, if you want to add a new header in suds, you should do something like this:

https = suds.transport.https.HttpTransport()
opener = urllib2.build_opener(HTTPSudsPreprocessor)
https.urlopener = opener
suds.client.Client(URL, transport = https)

where HTTPSudsPreprocessor is your own handler, and it should look like this:

class HTTPSudsPreprocessor(urllib2.BaseHandler):

    def http_request(self, req):
        req.add_header('Content-Type', 'text/xml; charset=utf-8')
        return req

    https_request = http_request

The methods you have to override depend on what you want to do. See urllib2 documentation in Python.org

Headman answered 19/5, 2010 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.