0

I want to create an API using DRF where I want to upload two files in two different name and a text field with a string of json. The figure below shows my postman attempt to the API.

enter image description here

Please help me to write my API properly. I looked for several posts in stack overflow but did not get any proper solution. I dont have any models according to the input but I want to create a standalone API and want to manipulate these files and json at runtime.

cjahangir
  • 1,773
  • 18
  • 27
  • Did you check these posts ? [Post-1](https://stackoverflow.com/questions/48756249/django-rest-uploading-and-serializing-multiple-images/48762785#48762785) and [Post-2](https://stackoverflow.com/questions/55315158/multiple-file-upload-drf/55354908#55354908) ?? – JPG Apr 29 '19 at 12:28
  • hey @JPG, thenk you but the examples you provided are with django models. I want a stand alone API which has no serialization, models or queryset. – cjahangir Apr 29 '19 at 12:33

1 Answers1

0

I assume that you are writing class based views. here is your view.py

class multipleFileUpload(APIView):

    def post(self, request):
    """

    :param request: 
    :return: 
    """
    try:
        #this will read attributes other than file
        str_value = request.POST["attributes"]
        print(str_value)

        #check if any file send in request if yes read all files one by one
        if len(request.FILES) != 0:
           for key in request.FILES:
               file_obj = request.FILES[key]
               print(file_obj.read()) #getting contents of the file in bytes
        return JsonResponse({"res": "got files"})
    except Exception as e:
        print(str(e))
        return JsonResponse({"res": "error"})

add this line in your urls.py

url(r'uploadFiles/$', views.multipleFileUpload.as_view(), name='uploadFiles'),

Run your Post man with the same parameter and let me know.

bSr
  • 1,410
  • 3
  • 16
  • 30