kartagis / pysimplesoap

Automatically exported from code.google.com/p/pysimplesoap
0 stars 0 forks source link

The 'ascii' codec can't encode character error when passing unicode strings to server #161

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create a simple soap server using the manual here 
http://code.google.com/p/pysimplesoap/wiki/SoapServer

2. Change one of the arguments from int to unicode

3. Run the server, generate a soap client for this server (I've used Visual 
Studio) and call it with some unicode characters in the string

Resulting error:  'ascii' codec can't encode character

I think I've tracked down the error to these lines in the unmarshall method in 
simplexml.py

            else:
                if fn is None:  # xsd:anyType not unmarshalled
                    value = node
                elif str(node) or (fn == str and str(node) != ''):
= ''):
                    try:
                        # get special deserialization function (if any)
                        fn = TYPE_UNMARSHAL_FN.get(fn, fn)
                        value = fn(str(node))
                    except (ValueError, TypeError) as e:
                        raise ValueError("Tag: %s: %s" % (name, e))

At the point "elif str(node)", the conversion str(node) fails if the string 
contains unicode chars. Replacing all str(node) calls to unicode(node) calls 
solves the problem.

There is also a workaround: I've defined my parameter as None instead of 
unicode. I then get a SimpleXmlElement in my method, and convert it using 
unicode(myparam).

I'm using the version 1.10 installed by pip install pysimplesoap

Original issue reported on code.google.com by g...@fridental.de on 16 Oct 2014 at 3:31

GoogleCodeExporter commented 8 years ago
I declare str for my strings parameters and decode to utf-8 
(str().decode('utf-8')) in the server side. This solve my problems with 
"'ascii' codec can't encode character ...". Best regards. Fernando.   

Original comment by fpacheco.ingesur@gmail.com on 28 Oct 2014 at 12:20

GoogleCodeExporter commented 8 years ago
I have the same problem. Here is a minimal test case:

client = SoapClient(wsdl='https://open.helios.eu/Demo43/data.asmx?WSDL')
url = client.GetInfo('GETREDIRECTINFO')['GetInfoResult']
url += '/servicegate.asmx?WSDL'
client = SoapClient(wsdl=url)
r = client.LogOn(profile='Demo43', username='tester', password='tester')

The error returned is:

  File "c:\Users\...\soap\env\lib\site-packages\pysimplesoap\simplexml.py", line 527, in unmarshall
    elif str(node) or (fn == str and str(node) != ''):
UnicodeEncodeError: 'ascii' codec can't encode character u'\xab' in position 
71: ordinal not in range(128)

Current master works for me. Could you please release new version?

Original comment by zbynek.winkler on 21 Nov 2014 at 3:25