-1

How to send file from the result of the request , if i write the file to a folder it works, but it takes time to write . but what i want is directly send from request result . sorry for my bad english

 import requests

 def get(cls, date: str, date_end: str, office_in_charge: str):
        user = get_jwt_identity()
        url = "http://localhost:9090/jasperserver/rest_v2/reports/reports/interactive/DTR.pdf"
        r = requests.get(url,
                         params={
                             'username': 'jasperadmin',
                             'password': 'jasperadmin',
                             'p1': user,
                             'p2': user,
                             'date': date,
                             'date_end': date_end,
                             'office_in_charge': office_in_charge
                         }
                         , allow_redirects=True)

        return send_file(r.content, as_attachment=True, mimetype='application/pdf')
blueelite
  • 319
  • 2
  • 7
  • 1
    `send_file` sends from a file. You want to send from the raw bytes. Write the response locally first, and then give the filename instead. Google it if you need help how to write to a file. – KTibow Jun 15 '20 at 16:33
  • thank's man that's i did but i dont what to write the file locally – blueelite Jun 15 '20 at 22:35
  • 1
    `r = requests.get(url) with open('file_name.pdf', 'wb') as f: f.write(r.content)` – KTibow Jun 15 '20 at 22:43
  • @KTibow thanks man but what i want is directly send the file from the request library – blueelite Jun 16 '20 at 00:49

1 Answers1

1

This might work for you:

 import requests, io

 def get(cls, date: str, date_end: str, office_in_charge: str):
        user = get_jwt_identity()
        url = "http://localhost:9090/jasperserver/rest_v2/reports/reports/interactive/DTR.pdf"
        r = requests.get(url,
                         params={
                             'username': 'jasperadmin',
                             'password': 'jasperadmin',
                             'p1': user,
                             'p2': user,
                             'date': date,
                             'date_end': date_end,
                             'office_in_charge': office_in_charge
                         }
                         , allow_redirects=True)

        return send_file(
                     io.BytesIO(r.content),
                     attachment_filename='report.pdf',
                     mimetype='application/pdf'
               )
KTibow
  • 495
  • 6
  • 18