1

I'm trying to upload image using django rest framework. But I had a problem with that , when I'm using postman form it upload image successfully, but when I'm trying to type json as row in postman it returns to me this error.

"The submitted data was not a file. Check the encoding type on the form."

here my code:

serializer.py

class UserImageCreateSerializer(serializers.HyperlinkedModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())

    class Meta:
        model = UserImages
        fields = ('user', 'image',)

view.py

class UserImageAPICreateView(ListCreateAPIView):
    queryset = UserImage.objects.all()
    serializer_class = UserImageCreateSerializer
    permission_classes = [AllowAny, AllowAnonymous]

my request:

{
"User": 79,
"image": "/path/to/image.jpg" }

note: when I use postman form it uploaded successfully and when use django rest framework HTML form it works too

I dont know what mistake I did.

so, any one has the solution please help me.

7
  • Actually, You can't upload any file as JSON. (application/json). It should be either form-data` or mulipart-form data Commented Oct 17, 2018 at 12:34
  • Aprt from that, why do you trying to upload image as raw JSON in POSTMAN? Commented Oct 17, 2018 at 12:35
  • thank you for your fast response. because I used this API's from IOS app used swift so i need to deal with json not postman. Commented Oct 17, 2018 at 12:36
  • BTW, I tried to use multipart-form but I think I have a lot to work with. If can give me a simple example or reference to deal with that point? Commented Oct 17, 2018 at 12:38
  • I'm not familiar with IOS app, did you try something like this with POSTMAN? i.sstatic.net/NqklQ.png Commented Oct 17, 2018 at 12:53

1 Answer 1

4

you can not upload a image file by just giving a file path in your json upload data like this:

{
"image": "/path/to/image.jpg"
}

If you send like this django will treat image data as normal string not a file

This is why you keep getting this error

"The submitted data was not a file. Check the encoding type on the form."

you need to send image data, not a image path.

here is python request example of upload image https://stackoverflow.com/a/45611449/2679465

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.