[Twisted-web] XMLRPC response encoding
Jean-Paul Calderone
exarkun at divmod.com
Mon Oct 22 08:10:32 EDT 2007
On Mon, 22 Oct 2007 07:55:49 -0400, Jean-Paul Calderone <exarkun at divmod.com> wrote:
> [snip]
>>How can I solve the encoding problem in the XMLRPC server response?
>
>Help resolve #1909. The patch attached to the ticket is at least missing
>unit tests for the new functionality it provides. If you can provide this,
>then we might be able to add the feature and resolve the ticket
Actually, this is wrong, please disregard it. :)
#1909 is misguided and the attached patch is incorrect. Specifying the
encoding parameter to xmlrpclib.dumps() doesn't do anything to solve the
problem here.
You either need to specify the encoding in the content-type header of the
response or you need to use UTF-8 (the default encoding for XML). Of these,
you can already do the latter without changing anything in Twisted, since
xmlrpclib will emit UTF-8 if you pass it unicode instead of already encoded
strings.
So the solution to your problem is to return unicode in your result, instead
of already encoded strings. These will be encoded to UTF-8, which will be
decoded properly by the client.
Compare:
>>> from xml.dom.minidom import parseString
>>> from xmlrpclib import dumps
>>> parseString(dumps((u'fòò',)))
<xml.dom.minidom.Document instance at 0xb7c0080c>
and
>>> parseString(dumps((u'fòò'.encode('latin-1'),), encoding='latin-1'))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/home/exarkun/.local/lib/python2.4/site-packages/_xmlplus/dom/minidom.py", line 1925, in parseString
return expatbuilder.parseString(string)
File "/home/exarkun/.local/lib/python2.4/site-packages/_xmlplus/dom/expatbuilder.py", line 942, in parseString
return builder.parseString(string)
File "/home/exarkun/.local/lib/python2.4/site-packages/_xmlplus/dom/expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 3, column 16
Jean-Paul
More information about the Twisted-web
mailing list