Search code examples
postmanmultipartform-dataform-data

Excel file and key, value pair being send in postman using form-data. Empty data coming


enter image description here

File is getting recieved but site_list is not being fetched when i type request.data.get('site_list'). Can someone suggest a way to capture both.


Solution

  • After some hours of trying I figured out the solution.

    So basically anytime we are sending more than one file/text Django automatically takes it as 'file' and shows everything under it. If you want to take the text/JSON, files separately then you have to create Model Forms.

    I will post mine here. Basically I wanted to accept an Excel File and Json text in the form-data and thats how i did it.

    And this is how I sent my data from postman enter image description here

    Forms.py

    from django import forms    
    class CustomerConfigUploadForm(forms.Form):
        files = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
        site_list = forms.CharField()
    

    views.py

     def post(self, request, filename, format=None):
        try:
            form = VariableUploadForm(request.POST,request.FILES)
                      
            # Validate the form data
            if form.is_valid():
    
                # Reading the uploaded file content
                file_obj = request.data['files']
                file_content = file_obj.read()
    
                # Parsing the Excel file using pandas
                df = pd.read_excel(file_content, engine='openpyxl', sheet_name='test_sheet')
                
                #Getting Customer, Site, and User from Request
                site_list = request.data.get('site_list')
    
    ...rest whatever you want to do..