Skip to content Skip to sidebar Skip to footer

How To Pass Image To Requests.post In Python?

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['fileToUpl

Solution 1:

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.

Post a Comment for "How To Pass Image To Requests.post In Python?"