Home > Blockchain >  How to send a PDF file by DRF
How to send a PDF file by DRF

Time:12-10

I have a request for you, can you please help me with one question. How can I send a pdf file via api. It starts like this, they send me the base64 format and I accept it and do the decoding. Then I need to send this file to another endpoint, but I just can’t put it there. Could you please help me

enter image description here According to Postman, my file sits quietly and works as it should. The picture shows that it takes the form-date.

def sendDocs(photoBack):
    try:
        headers = {'Content-Type':'multipart/form-data;', 
                   'bsauth': 'key'}
        import base64
        decodedData = base64.b64decode((photoBack))
        pdfFile = open('photoBack.pdf', 'rb ')
        pdfFile.write(decodedData)
        pdfFile.close()
        response = HttpResponse(pdfFile, content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="photoBack.pdf"'
        url = f'https://file.shinhanfinance.kz/files/shinhanfinance/add?client={1013246509}'
        files = {"file":response}
        firstPost1 = requests.post(url,data =response,headers=headers)
        print(Response(firstPost1))
        return Response({firstPost1})
     
    except:
        return Response({'bruh what wrong ?'})

Here my code

CodePudding user response:

def sendDocs(photoBack):
    try:
        headers = {'Content-Type':'multipart/form-data;', 
                   'bsauth': 'key'}
        import base64
        decodedData = base64.b64decode((photoBack))
        pdfFile = open('photoBack.pdf', 'rb ')
        pdfFile.write(decodedData)
        pdfFile.close()
        response = HttpResponse(pdfFile, content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="photoBack.pdf"'
        url = f'https://file.shinhanfinance.kz/files/shinhanfinance/add?client={1013246509}'
        files = {"file":response}
        firstPost1 = requests.post(url,data =response,headers=headers)
        print(Response(firstPost1))
        return Response({firstPost1})
     
    except:
        return Response({'bruh what wrong ?'})

CodePudding user response:

You can try to use decodedData, and put it into file parameters.

firstPost1 = requests.post(url, files={"file": decodedData}, headers=headers)

I think here is your solution How to upload file with python requests?

  • Related