3

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?

4
  • What format is your PDF in? Commented Jan 27, 2021 at 12:54
  • (it looks like base64 inside JSON. Do you know how to decode those 2 types?) Commented Jan 27, 2021 at 12:54
  • The response.content is the whole JSON. And you probably only need the value under the "data" key. So convert it to the dictionary and get the value. Commented Jan 27, 2021 at 12:54
  • Try f.write(response.json()['data']) Commented Jan 27, 2021 at 12:55

2 Answers 2

4

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...

Sign up to request clarification or add additional context in comments.

Comments

3

You try to write json to a file. You should parse your json and write something like

data = response.json()["data"]
with open("my_file.pdf", 'wb') as f:
    f.write(base64.b64decode(data))
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.