I have a model with API for an ImageField. I need to send image fetched via post method on the template and send it via post request to the API created. The image fetched has a type InMemoryUploadedFile
, if I try to send it directly, I get 406 because of failed serializer validation. So I tried making a PIL object out of it and tried sending. I checked the JS counterpart of the code and it just takes the file from the input field and sends it directly to the same field and it works.
Is there a way I can just send an image file object via post request to fail serializer validation.
category_thumbnail = request.FILES.get('category_thumbnail')
category_thumbnail = Image.open(category_thumbnail)
data = {
'category_thumbnail': category_thumbnail
}
This would give me 406.
I also tried converting image string to a base64 byte object.
category_thumbnail = request.FILES.get('category_thumbnail')
category_thumbnail = Image.open(category_thumbnail)
with io.BytesIO() as output:
category_thumbnail.save(output, format="GIF")
contents = output.getvalue()
category_thumbnail = base64.b64decode(contents)
data = {
'category_thumbnail': category_thumbnail
}
but this would give me 406 too.
I wonder if there's a way I can just send the image file object taken from InMemoryUploadedFile
.
Also tried
category_thumbnail = request.FILES.get('category_thumbnail').file