Reputation: 37
I'm trying to Post, from Postman, multiple files to my django app. I'm not using Forms, and there isn't a UI aspect to my app. Here is a my view class.
class FileUploader(APIView):
'''
Rest API for FileUploader
'''
permission_classes = (AllowAny,)
parser_classes = (MultiPartParser, )
@csrf_exempt
def post(self, request):
retval = Response(request.data, status=status.HTTP_201_CREATED)
logger.info('New post with the following data: {}'.format(request.data))
With this it says, "TypeError: init() missing 3 required positional arguments: 'META', 'input_data', and 'upload_handlers'" If I use FormView, my Post has three keys, two represent files, the last is a string. During debugging my request has no field Data, and FILES is empty, and the POST doesn't have any information. Any pointers would be appreciated. I can upload more if that helps.
It's not a duplicate because he was able to upload multiple files and mine doesn't upload any files. I'm struggling to figure out how to find the files within the request and since they aren't there how to set up the views (and not the serialize) to receive multiple files.
Upvotes: 1
Views: 3899
Reputation: 39
I have the same problems as you.
But this problem cause by my mistake when import package.
I should import MultiPartParser
from rest_framework.parsers
instead of from django.http.multipartparser
from rest_framework.parsers import MultiPartParser
from django.http.multipartparser import MultiPartParser
Upvotes: 1
Reputation: 88629
Write a view class as
from rest_framework.views import APIView
from rest_framework.response import Response
class FileUploader(APIView):
'''
Rest API for FileUploader
'''
permission_classes = (AllowAny,)
def post(self, request, *args, **kwargs):
files_list = request.FILES
data = request.data
return Response(data={"files": "{} files uploaded".format(len(files_list)),
"data": "{} data included".format(len(data))})
and send it using form-data
in POSTMAN
Upvotes: 2
Reputation: 11695
change the above code to like below and include header 'Content-Type': 'multipart/form-data'
in the request.
class FileUploader(APIView):
'''
Rest API for FileUploader
'''
permission_classes = (AllowAny,)
parser_classes = (MultiPartParser, )
@csrf_exempt
def post(self, request, *args, **kwargs):
print(request.data)
return Response({"message": "success"})
Upvotes: 0