0

I have this at django

@csrf_exempt  
@require_http_methods(['POST'])  
@api_token  
def foo(request):
    upload_file = request.FILES['file']
    random_function(upload_file)
    return HttpResponse('done')

and this at another python file

import requests

url = "http://127.0.0.1:8000/api/external/path"

payload = {}
files = {'file': open('csv_file.csv', 'rb')}
headers = {
    'x-api-key': 'api-key-token',
    'Content-Type': 'application/form-data'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text.encode('utf8'))

the problem is that when I executed the python file I got this error

django.utils.datastructures.MultiValueDictKeyError: 'file'

it was like request.FILES can't find file name but when I execute it from postman it works fine

Peem
  • 41
  • 7

1 Answers1

0

Okay guys, I've tried

headers = {
    'x-api-key': 'api-key-token'
}

And then it works. My team said because the boundary has gone missing since we force the content type to be multipart/form-data but I still don't quite get it yet. If anyone can explain it would be much appreciated

Peem
  • 41
  • 7