What's the correct way to deal with render_GET and unicode?

If I return a unicode string from my Resource's render_GET, I get a 500 error that says "Request did not return a string". I guess that means I'm responsible for encoding the response, but do I then need to write a render_HEAD to set the charset for the message? Are there any examples of how to do this? --- Mark Wright markscottwright@gmail.com

On Aug 27, 2009, at 12:02 PM, Mark Wright wrote:
If I return a unicode string from my Resource's render_GET, I get a 500 error that says "Request did not return a string". I guess that means I'm responsible for encoding the response, but do I then need to write a render_HEAD to set the charset for the message? Are there any examples of how to do this?
Assuming you're using Twisted.Web, you should just be able to set the encoding via the Request object, like: req.setHeader('Content-Type', 'text/html; charset=UTF-8') -phil

On 04:02 pm, markscottwright@gmail.com wrote:
If I return a unicode string from my Resource's render_GET, I get a 500 error that says "Request did not return a string". I guess that means I'm responsible for encoding the response, but do I then need to write a render_HEAD to set the charset for the message? Are there any examples of how to do this?
Not a render_HEAD. You do need to set the Content-Type header in the response, but you can do that with request.setHeader (or on a very new version of Twisted, request.responseHeaders.addRawHeader). For example, def render_GET(self, request): request.setHeader("Content-Type", "text/plain; charset=utf-8") return u"\N{SNOWMAN}".encode('utf-8') Jean-Paul
participants (3)
-
exarkun@twistedmatrix.com
-
Mark Wright
-
Phil Christensen