Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Encoding and decoding arbitrary strings into strings that are safe to put into a URL query param.

Project Description

[![Build Status](]( [![Coverage Status](](

# Query string safe Base64

Encoding and decoding arbitrary strings into strings that are safe to put into a URL query param.

## The problem

urlsafe_b64encode and urlsafe_b64decode from base64 are not enough because they leave = chars unquoted:

>>> import base64
>>> base64.urlsafe_b64encode('a')
>>> 'YQ=='

And there are 2 problems with that

  1. = sign gets quoted:

    >>> import urllib
    >>> urllib.quote('=')
  2. Some libraries tolerate the = in query string values:

    >>> from urlparse import urlsplit, parse_qs
    >>> parse_qs(urlsplit('').query)
    {'q': ['AAAA=BBBB=CCCC']}

but the RFC 3986 underspecifies the query string so we cannot rely on = chars being handled by all web applications as it is done by urlparse.

Therefore we consider chars: ['+', '/', '='] unsafe and we replace them with ['-', '_', '.']. Characters + and / are already handled by urlsafe_* functions from base64 so only = is left for us. The . character has been chosen because it often appears in real world query strings and it is not used by base64.

## The solution

>>> import querystringsafe_base64
>>> querystringsafe_base64.encode('foo-bar')
>>> 'Zm9vLWJhcg..'
>>> querystringsafe_base64.decode('Zm9vLWJhcg..')
>>> 'foo-bar'
Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
querystringsafe_base64-0.2.0-py2.6.egg (4.3 kB) Copy SHA256 Checksum SHA256 2.6 Egg Feb 10, 2016
querystringsafe_base64-0.2.0-py2.7.egg (4.3 kB) Copy SHA256 Checksum SHA256 2.7 Egg Feb 10, 2016
querystringsafe_base64-0.2.0-py3.3.egg (4.4 kB) Copy SHA256 Checksum SHA256 3.3 Egg Feb 10, 2016
querystringsafe_base64-0.2.0-py3.4.egg (4.4 kB) Copy SHA256 Checksum SHA256 3.4 Egg Feb 10, 2016
querystringsafe_base64-0.2.0-py3.5.egg (4.4 kB) Copy SHA256 Checksum SHA256 3.5 Egg Feb 10, 2016
querystringsafe_base64-0.2.0.tar.gz (4.0 kB) Copy SHA256 Checksum SHA256 Source Feb 10, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting