0

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

coder
  • 31
  • 1
  • 7
  • What is the server expecting for the field 'category_tumbnail'? – Akash Rudra May 12 '21 at 05:45
  • https://stackoverflow.com/questions/45564130/django-rest-framework-image-upload ? – JPG May 12 '21 at 06:13
  • @AkashRudra it is an ImageField and in the API it's just validating through the serializer. So an image type object. – coder May 12 '21 at 06:46
  • @JPG that's an issue he's facing on the API side. My problem is on the view side. I need to send image as a parameter and not receive one. – coder May 12 '21 at 06:50

1 Answers1

0

if the code is in API, you should use request.data instead of request.FILES if the code is in normal html view, like a form. then it should be using an HTML form with enctype="multipart/form-data" or FormData generated by javascripts.

And in views:

obj.category_thumbnail = request.FILES['category_thumbnail']

it should be used directly with rest_framework, no matter it is InMemoryUploadedFile or UploadedFile.

If, for some reason, you are using serializer rather than form in view. Then the view should be derived from APIView if class based. Or use @api_view decorator to function based view.

Mas Zero
  • 503
  • 3
  • 16
  • Your soution again is on the API side. The API shows accepted with 200 when sending files with postman or JS. I need to send *Image* type file via json to the API created from my view. – coder May 12 '21 at 08:11
  • @coder you say it is not about API side, then what do you mean `to API created from my view`? What does the `view` do? Mentioned that If it is a django standard view, then the standard way is to use form. Or it shall be an APIView or @api_view. – Mas Zero May 12 '21 at 08:18
  • @coder Your codes in the post does not throw exceptions at all. The Exception shall be when you try to validate and save the image with the `serializer` right? – Mas Zero May 12 '21 at 08:24
  • my bad. The API is created and hosted on a different server. What I'm doing is independent of that server. I'm hosting this code on different machine and sending post request to the API which is created on a different server. Let's just consider this as a python problem. – coder May 12 '21 at 09:21
  • Maybe I am totally missed... Is your question "How to send submitted image using python?" – Mas Zero May 12 '21 at 20:22