marselabdullin2020-07-25 16:03:45
marselabdullin, 2020-07-25 16:03:45

400 error when passing blob file via rest?

The file is transferred from vue.js, the picture is cropped there and transferred in blob format, however, the field type of the model into which the blob is written is imagefield, and I suppose this causes a 400 error, but in the serializer I convert blob to imagefield, but apparently an error occurs before this point, since neither views nor serializers write anything to the logs. What can be done in such a case?


def create(self, request, *args, **kwargs):
        if not request.data:
            logger.error("Not request.data")
            return response.Response({"result": "data can`t be a blanks"}, status.HTTP_400_BAD_REQUEST)
        serializer = self.get_serializer(data=request.data)
        return response.Response(serializer.data, status=status.HTTP_201_CREATED)

def create(self, validated_data):
        user = User.objects.get(email=validated_data['email'])

        with io.BytesIO(self.context.get('view').request.FILES['image']) as stream:
            django_file = File(stream)
            user.image.save('{}_profile_image'.format(user.email), django_file)

        return user.save()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Dr. Bacon, 2020-07-25

Why so many perversions? The idea is that the following code should work

user.image = self.context.get('view').request.FILES['image']

return user.save()what else is this? It is necessary to return the object itself, and not the result of the work of the save function
And by mistake, debug, catch the places where it occurs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question