-1

Sorry Guys, I am new to Django, I am stuck with images upload.

I have a REST_API for image upload. I pass the image and inside API get that image by using request.FILES['fileToUpload'].

Now I have an external API, which uploads an image on my behalf, that API is working fine on the postman.

Here is the path of that API.

http://164.68.110.65/file_load.php

But in Django. I am not able to pass the image to this API. I have tried many ways.

like these.

 image = request.FILES['fileToUpload']
 temp = Image.open(image)
 byte_io = BytesIO()
 temp.save(byte_io, 'png')
 files = {'fileToUpload': byte_io.getvalue() }
 response = requests.post( self.URL, files=files)
 print(response.status_code, response.content, response.reason)

but it always giving me an error that image format is not matched.

can you please tell me, in python-requests, how we should pass images or files that are got my request.FILES['file_name_any'].

Thanks. I will be very thankful for your favor.

etrupja
  • 2,710
  • 6
  • 22
  • 37
Amad
  • 314
  • 1
  • 6
  • 25

1 Answers1

0

Your image is an UploadedFile or a TemporaryUploadedFile which is a subclass of File. So you can just .open() it normally as any other File object:

with image.open('rb') as f:
    files = {'fileToUpload': f}
response = requests.post(self.URL, files=files)

No need to take the detour through saving the file first.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42