[Answered ]-Django rest frame work – File upload fails in test but working everywhere else

2πŸ‘

βœ…

@mariodev answered this in the comments yesterday:

Have you tried the rb flag instead of r?

This is because otherwise you uploading the file with an encoding. For text files, and other non-binary files, this shouldn’t be an issue, but for cases such as image files you need to open it as a binary file.

temp_file = open('hm.jpg',"rb")

When Django REST Framework checks that the image is valid using Pillow, it will raise an exception if the encoding is wrong. This is why you are getting the error, as Django REST Framework got a file upload, but can’t verify that it is an image. The encoding was correct when using curl, because curl was uploading it as a binary file and was handling it all for you.

Leave a comment