Django REST Framework File Upload

  • Posted on: 26 December 2015
  • By: oon
Django REST Framework

This article inspired from same title article by jxstanford on [1].

so, what's the difference?

One of the main difference is I use JWT Authentication (django-rest-framework-jwt [2]) instead of Token Authentication.

so here's my code


from rest_framework import status
# from rest_framework.authtoken.models import Token
from rest_framework_jwt.settings import api_settings
from rest_framework.test import APITestCase
from rest_framework.test import APIClient
from django.core import serializers
from django.core.urlresolvers import reverse
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User
from myapp.models import FileUpload
from myproject import settings
from urlparse import urlparse

class FileUploadTests(APITestCase):

    def setUp(self):
        u = User.objects.create_user('test', password='test', email='test@test.test')
        # print(serializers.serialize('json', User.objects.all()))

    def tearDown(self):
            u = User.objects.get_by_natural_key('test')

        except ObjectDoesNotExist:

    def _create_test_file(self, path):
        f = open(path, 'w')
        f = open(path, 'rb')
        return {'datafile': f}

    def test_upload_file(self):
        url = reverse('fileupload-list')
        data = self._create_test_file('/tmp/test_upload')

        # assert authenticated user can upload file
        # token = Token.objects.get(user__username='test')
        # client = APIClient()
        # client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)

        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        user = User.objects.get_by_natural_key('test')
        payload = jwt_payload_handler(user)
        jwt_token = jwt_encode_handler(payload)

        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION='JWT ' + jwt_token)

        response =, data, format='multipart')
        # print(
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(['owner'], User.objects.get_by_natural_key('test').id)

        # assert unauthenticated user can not upload file
        response =, data, format='multipart')
        # print(
        # self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)  # JWT Authentication credentials not provided

hope it will help someone out there..


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.