django image upload rest framework and test client
<p>I'm trying to upload a picture with rest framework and to test it with the django test client. It almost works, however when I save the file I have this on the top of my file:</p>

<pre><code>--BoUnDaRyStRiNg
Content-Disposition: form-data; name="plop.png"
</code></pre>

<p>If I delete those lines, I can perfectly read my picture, but I don't know how to rid of them.</p>

<p>models.py</p>

<pre><code>class GenericUser(User):
    avatar = models.ImageField(upload_to=user_directory_path)
</code></pre>

<p>views.py</p>

<pre><code>class AvatarView(APIView):
    parser_classes = (FileUploadParser,)
    def post(self, request):
        up_file = request.data['file']
        user = get_object_or_404(GenericUser, pk=request.user.id)
        user.avatar.save(up_file.name, up_file)
        user.save()
        return Response(status=204)
</code></pre>

<p>tests.py</p>

<pre><code>cl = Client(HTTP_AUTHORIZATION="Token {}".format(token))
with open(MY_FILE, 'rb') as f:
    data = f.read()
res = cl.post('/avatar', {"plop.png" : data}, HTTP_CONTENT_DISPOSITION="attachment; filename=plop.png;")
</code></pre>

<p>any hints, how I could make this work?</p>

<p>Try this:</p>

<pre><code>res = cl.post('/avatar?filename=plop.png', {"plop.png" : data})
</code></pre>

<p>Or more or less from the Django docs:</p>

<pre><code>with open(MY_FILE, 'rb') as f:
    cl.post('/avatar', {"filename" : f})
</code></pre>

python django django-rest-framework