Character Encodings

WebStack tries to let applications work with Unicode as much as possible, but there are two places where plain Python strings can be involved:

Recommendations

Although WebStack has some support for detecting character encodings used in requests, it is often best for your application to exercise control over which encoding is used when inspecting request parameters and when producing responses. The best way to do this is to decide which encoding is most suitable for the data presented and received in your application and then to use it throughout. Here is an outline of code which does this:

from WebStack.Generic import ContentType

class MyResource:

    encoding = "utf-8"                                                     # We decide on "utf-8" as our chosen
                                                                           # encoding.
    def respond(self, trans):
        [Do various things.]

        fields = trans.get_fields_from_body(encoding=self.encoding)        # Explicitly use the encoding.

        [Do other things with the Unicode values from the fields.]

        trans.set_content_type(ContentType("text/html", self.encoding))    # The output Web page uses the encoding.

        [Produce the response, making sure that self.encoding is used to convert Unicode to raw strings.]