python-zeep
python-zeep copied to clipboard
"latin-1 codec cant encode characters" when trying to send request
Hi! I'm a total beginner with SOAP requests, especially... with SOAP requests to a resource in 'latin-1' encoding. The thing is, I have an "1C" database which initial encoding is 'latin-1' and I need to retrieve some information from there. I can successfully establish a connection to the db, but once I'm sending any request, it raises an Exception with "latin-1 codec cant encode characters in ...". which is I guess happening on the moment when Zeep is reading the response from the db and returning it back to me. Long story short, is there any possibility to solve this? Like, set default encoding before sending request, or something like that. Perfectly, I'd like to get the result back in utf-8, of course.
Any help will be appreciated, as I'm going nuts.
Do you have any more specific information? Are you able to follow the steps here to obtain the response from the server?
how did you fix it? Facing same issue
In general - the problem is not in the zeep module
zeep send a request using the requests library. And, in turn, uses the standard python library - http. The http module encodes headers using latin-1

from zeep.wsdl.messages.soap import SoapMessage
def changed_serialize(f):
def wrapper(*args, **kwargs):
result = f(*args, **kwargs)
result.headers["SOAPAction"] = result.headers["SOAPAction"].encode("utf-8")
return result
return wrapper
SoapMessage.serialize = changed_serialize(SoapMessage.serialize)
I wrote this wrapper, which encode header "SOAPAction" to utf-8 and this work for me. Please set it in your library code (maybe as setter for SerializedMessage.headers - encode headers values to ust-8)