Python 从request.FILES检索文件时出现问题
我在django有这个Python 从request.FILES检索文件时出现问题,python,django,request,Python,Django,Request,我在django有这个 @csrf_exempt @require_http_methods(['POST']) @api_token def foo(request): upload_file = request.FILES['file'] random_function(upload_file) return HttpResponse('done') 这是另一个python文件 import requests url = "http://127.0.0
@csrf_exempt
@require_http_methods(['POST'])
@api_token
def foo(request):
upload_file = request.FILES['file']
random_function(upload_file)
return HttpResponse('done')
这是另一个python文件
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'))
问题是,当我执行python文件时,我遇到了这个错误
django.utils.datastructures.MultiValueDictKeyError:“文件”
这就像是请求。文件
找不到文件
名称
但是当我从邮递员那里执行它时,效果很好好了,伙计们,我已经试过了
headers = {
'x-api-key': 'api-key-token'
}
然后它就起作用了。我的团队说,因为自从我们强制内容类型为
multipart/formdata
后,边界就消失了,但我还是不太明白。如果有人能解释一下,我将不胜感激您是否尝试过使用“内容类型”:“多部分/表单数据”?我尝试过,但仍然出现同样的错误