Encoding and Decoding Path Values

On some occasions it can be necessary to manually decode path values, producing genuine Unicode objects, and then to encode them, producing plain strings that can be used in response headers and other places. For such occasions, some transaction methods are available:

WebStack API - Encoding and Decoding Path Values

WebStack provides the following methods to transform path values:

decode_path
This method accepts a path containing "URL encoded" information (as defined in the "URLs and Paths" document) and, using an optional encoding parameter, returns a Unicode object containing genuine character values in place of the "URL encoded" values.
encode_path
This method accepts a Unicode object containing the path and an optional encoding parameter; it reverses the process carried out by the decode_path method.

Generally, the decode_path method is of little interest; its only relatively common application might be to decode query strings:

qs = trans.get_query_string()                # eg. "a=%E6"
new_qs = trans.decode_path(qs, "iso-8859-1") # producing "a=æ"

Such operations are generally better performed using the request parameter methods.

The encode_path method is slightly more useful: since various transaction methods return values which have already been transformed into Unicode objects, we must consider the use of encode_path to produce values which are suitable for feeding into other methods. For example, having obtained a path, we may wish to cause a redirect to another location based on that path:

path = trans.get_path_without_query("iso-8859-1") # eg. "/app/resource"
path += "/æøå"
new_path = trans.encode_path(path, "iso-8859-1") # producing "/app/resource/%E6%F8%E5"
trans.redirect(new_path)

It is essential to encode the path in such situations because the underlying mechanisms do not support the full range of Unicode characters. Some cases where this limitation exists are listed in the "Character Encodings" document.