Django REST Framework file upload causing an "Unsupported media type 'multipart/form-data'" error
Solution 1
You should use the MultiPartParser instead of the FormParser if you're sending multipart/form-data.
Solution 2
Raised if there are no parsers that can handle the content type of the request data when accessing request.DATA or request.FILES.
check Django REST Framework2 documentation
import suitable parser
from rest_framework.parsers import MultiPartParser, FormParser, JSONParser
class SampleView(APIView):
parser_classes = (MultiPartParser,FormParser,JSONParser)
Md. Tanvir Raihan
A software developer who mostly does back-end coding and enjoy playing with the logic's. Opensource enthusiast, love to contribute in the community
Updated on June 26, 2022Comments
-
Md. Tanvir Raihan almost 2 years
I am newbie in Django and Django REST Framework. I have the following serializer class which I am using to upload a file along other information. But, while I run the API endpoint with uploaded file, the result is something like this:
HTTP 415 Unsupported Media Type Allow: POST, OPTIONS Content-Type: application/json Vary: Accept { "detail": "Unsupported media type \"multipart/form-data; boundary=----WebKitFormBoundaryybZ07gjZAqvcsZw3\" in request." }
I tried hard by googling to solve this issue, but cannot come out in a solution, so here is my serializer and API views.
Serializer:
class ExampleSerializer(serializers.Serializer): example_id = serializers.IntegerField() description = serializers.CharField(allow_blank=True) example_file = serializers.FileField(allow_empty_file=True) def create_requirement_line(self): request = self.context['request'] requirement_line = ExampleService().example_method( example_id=self.validated_data['example_id'], description=self.validated_data['description'], example_file=self.validated_data['example_file'] ) return requirement_line
View:
class RequirementLineAPIView(BaseCreateAPIView): serializer_class = ExampleSerializer parser_classes = (FormParser,) def post(self, request, format=None,*args, **kwargs): serializer = self.get_serializer(data=request.data) if serializer.is_valid(): try: example_variable = serializer.example_method() return Response(example_variable, status=status.HTTP_200_OK) except ValidationError as e: return Response(e.message, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
-
Md. Tanvir Raihan about 8 yearsthanks for your answer, if i use
MultiPartParser
then i am getting the following errorUnicodeDecodeError at /api/requirement-header-line/ 'ascii' codec can't decode byte 0xff in position 76: ordinal not in range(128)
in mention , i have applied a customize service method,is there something that i have to do in my service method? -
Ivan Genchev about 8 yearsCan you provide some more information? Like the data you're trying to send?
-
Md. Tanvir Raihan about 8 yearsok,to be specific,i am sending an integer(say 30) as my
example_id
, and 'This is a description' as adescription
and a .pptx or a .pdf file as aexample_file
. @Ivan Genchev -
Ivan Genchev about 8 yearsThat should fix it: REST_FRAMEWORK = { ... 'UNICODE_JSON': False }
-
Ivan Genchev about 8 yearsbut it's not related to your original issue and also that issue depends on the settings, version of python and version of drf, that you're using...
-
Md. Tanvir Raihan about 8 yearsok,after setting that REST_FRAMEWORK = { ... 'UNICODE_JSON': False },now i am gettting the following error,
'utf8' codec can't decode byte 0xff in position 14: invalid start byte
, in mention i am using python2.7 and DRF3.x and django 1.9 -
Md. Tanvir Raihan about 8 yearsThanks,your shared link really helped to understand whats the problem,technically your answer is right and i am accepting it.