Skip to content

Instantly share code, notes, and snippets.

@mredar
Last active September 26, 2019 11:41
Show Gist options
  • Save mredar/5681352 to your computer and use it in GitHub Desktop.
Save mredar/5681352 to your computer and use it in GitHub Desktop.
Decoding unpadded base64 data (e.g. SQS messages) in python. You need to pad out the string, found this snippet on: http://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding
>>> import base64
>>> s='eyJ1cmwiOiAiaHR0cDovL2RpZ2l0YWwyLmxpYnJhcnkudWNsYS5lZHUvb2FpMl8wLmRvIiwgI
nNldF9zcGVjIjogImFpZHNwb3N0ZXJzIiwgImNhbXB1cyI6IFt7InNsdWciOiAiVUNMQSIsICJuYW1lI
jogIlVDTEEiLCAicmVzb3VyY2VfdXJpIjogIi9hcGkvdjEvY2FtcHVzLzEwLyJ9XX0'
>>> d=base64.b64decode(s)
Traceback (most recent call last):
...
TypeError: Incorrect padding
>>> d=base64.decodestring(s + '='*(-len(s) % 4))
>>> d
'{"url": "http://digital2.library.ucla.edu/oai2_0.do", "set_spec": "aidsposters"
, "campus": [{"slug": "UCLA", "name": "UCLA", "resource_uri": "/api/v1/campus/10
/"}]}'
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment