via requests I receive the following PDF document, that I want to save:
b'{"data":"JVBERi0xLjUKJeLjz9MKNSAwIG9iago8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDIxNTk+PnN0cmVhbQp4nOVaS28URxBuybe52Mhv8GEOdsBYaU/PdM+DiEtkwDjEYLDBIfhCEEFoJxG55J/kmCM/I78t1c+prra9szZIkaL1rrd3q7/6uuqrfszOp+z746zIWyl42+bH77IHx9lR9ikreFE1Kv8zK/MD+P5jJor8x+znsyJ/Z7/3XXp4W+STTHWKly1+6wwm2YfsVfab7pWLXBvr12/BgZRdl//S57un70W+93uucbGVwbnM0rgu8j9+1YPYfSjyFljnx++zgFBoBCG4yI/77A6r2T57wl7B69z28Uc91os6VgUXDeo5x35iz9nR1H6l4qpG/Zam2MuWC4nsl529LLmq8kbVvK0gxkqVHNLh2hPfLlteddC01nEr2NrwFxyS454QsLIFozKvmorXChzYPq45Qc1ON5G1aX/I3meVKHknB4QYMe2D7AeMWnDIbMCIMdM+yD5gSCW4rBFGhJn2QfYBQxWCNwJFI8JM+yD7AcOmJGDEmGkfZD+MBYe9T0JIQ0ySphG8iax40Q15tc0JahqlIGvTxgieRJIDkiNKCmvDs4hZpX6RfeAR++2TPCZ5JrywvjyPmFfqF9kPPCK/faIFqhXKC2s08Ih4pX6RfeAR++1TPRG9UV5Y50EdEa/UL7IPPGK/faLjuA5SXrhWPI+YV+oX2Q/xwFLuE0lRyZFCQEovu46XQ6245gQ1FQIMbVJtFdEX1TXVfUF0HljErFK/yF7RWgk8iJ4SvRFeSOeBR8wr9YvsFa0Vz4PqieqN8kI6H3hEvFK/yF7RWvE8Ej0RvVFeSOeDOiJeqV9krwjGwIPoOK6DlBeqlcAj5pX6RfZDPLCU+0RSVHKkEHCt1C1vyqFW ..... and much more wOTgzMjYgMDAwMDAgbiAKMDAwMDA5ODM2NyAwMDAwMCBuIAowMDAwMDk4NTUyIDAwMDAwIG4gCnRyYWlsZXIKPDwvSW5mbyA0MiAwIFIvSUQgWzw4MWNmZjFlOWRjZjlmMzcxZjJkYzNmZTllYWY0MTI1MD48NjNmN2EzMTViNjk5MDBiY2YzZDcxOTUzY2MzZDFmNWQ+XS9Sb290IDQxIDAgUi9TaXplIDQzPj4Kc3RhcnR4cmVmCjk4NTk5CiUlRU9GCg==","documentTemplateType":1459425196590,"mimeType":"application/pdf","title":"title of the pdf"}'
I tried saving it via:
response = requests.get(url, headers=self.get_authentication_header(path=path, method="GET")) with open("my_file.pdf", 'wb') as f: f.write(response.content) f.close()
which works, but the pdf is corrupt. How do I save this PDF?
Advertisement
Answer
It seems like base64-encoded (described in RFC 3548) data inside JSON, try following:
import base64 ... data = response.json()["data"] with open("my_file.pdf", 'wb') as f: f.write(base64.b64decode(data))
As side note: you do not need to close
file explicitly if you use with open
…