Handling Spaces in Query Parameters Between Python and Anvil

I’m integrating an old (non-Anvil) Python app with an Anvil app. The legacy app constructs URLs with query parameters using:

url += '#?' + urllib.parse.urlencode(params)

This means that spaces in parameter values are encoded as + , following the standard Python and URL behavior for query strings.

When my Anvil app starts, I use get_url_hash() to retrieve the fragment (everything after the # ). However, when I parse this hash for parameters, any + characters are not converted back to spaces. This feels like a mismatch between Python’s default encoding and Anvil’s default parsing or decoding behavior.

Current Workaround

I can work around this issue by using:

urllib.parse.urlencode(params, quote_via=urllib.parse.quote)

on the sending side, which encodes spaces as %20 instead of + . But this doesn’t feel like the right solution, especially since this workaround becomes confusing—I’m only required to use it for calls to Anvil, not for other URLs or services.

Main Question

Is there an Anvil-recommended way to parse query or hash parameters so that + is correctly decoded as a space? Or is it best practice to always force %20 encoding on the sending side when integrating with Anvil?

Any advice or recommended best practices would be appreciated!

2 Likes