python-zeep icon indicating copy to clipboard operation
python-zeep copied to clipboard

"latin-1 codec cant encode characters" when trying to send request

Open yellalena opened this issue 5 years ago • 4 comments

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.

yellalena avatar Aug 26 '20 22:08 yellalena

Do you have any more specific information? Are you able to follow the steps here to obtain the response from the server?

da1910 avatar Aug 31 '20 20:08 da1910

how did you fix it? Facing same issue

Simiezi avatar Jul 13 '21 06:07 Simiezi

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 image

Eff1c avatar Feb 21 '22 12:02 Eff1c

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)

Eff1c avatar Feb 23 '22 10:02 Eff1c