GatewayNode

Web Suitable Base64 in Python

So I was hacking away at JSON Web Tokens the other day and I came across an annoying bit of Python, which is strange as I usually love working in Python more than any other programming language. Apparently the Python base64 implementation is just fine for decoding base64 strings from the web, but to encode data structures back to web suitable base64 you have to do this:

web_suitable_data = base64.b64encode(bytes(json.dumps(data, separators=(',', ':')), 'utf-8')).decode('utf-8')

It wasn't very hard to figure out, just an unusual quirk in the language. Not very Pythonic, kind of feels like Java, made me feel dirty.

Ouroboros, you know the snake that eats itself...