Python 从“发送文件到外部api”;InMemoryUploadedFile“;在Django
在Django框架中,我希望在收到一个作为InMemoryUploadedFile接收的文件后,立即将其发布到另一台服务器 我曾在中尝试过该解决方案,但该解决方案对我无效 外部API给出的Curl请求:Python 从“发送文件到外部api”;InMemoryUploadedFile“;在Django,python,python-3.x,django,django-3.1,Python,Python 3.x,Django,Django 3.1,在Django框架中,我希望在收到一个作为InMemoryUploadedFile接收的文件后,立即将其发布到另一台服务器 我曾在中尝试过该解决方案,但该解决方案对我无效 外部API给出的Curl请求: curl --location --request POST 'https://app-sh.exmp.com/api/cret' \ --header 'Content-Type: multipart/form-data' \ --header 'api-key: Aa4f*******' \
curl --location --request POST 'https://app-sh.exmp.com/api/cret' \
--header 'Content-Type: multipart/form-data' \
--header 'api-key: Aa4f*******' \
--form 'kyc_doc=@/C:/Users/Shine/Downloads/image.png' \
--form 'company_name=name' \
--form 'company_country=IN' \
--form 'email=abc@xyz.com'
如何将以下内容转换为request.post()
从我当前的函数流来看,该文件没有发送到外部api。这是我的密码:
views.py
def processUpload(request):
data['company_country'] = request.POST.get('company_country')
data['company_name'] = request.POST.get('company_name')
#upload file
data['kyc_doc'] = request.FILES['kyc_doc'].file.getvalue()
files = {"kyc_doc":request.FILES['kyc_doc'].file.getvalue()}
return service.doCurl(data,files)
def doCurl(data,files):
#.....
# business logic here
#
del data['common_user_token']
response_kcloud = curl.post('https://app-sh.exmp.com/api/cret',data,{ "api-key":acc.api_key},files)
def post(url,data,headers,ufile=None):
curlheaders = {"Content-Type": "application/json"}
#empty data
if not data:
raise Exception("data cannot be empty")
#empty url
if not url:
raise Exception("Url cannot be empty")
#base case for header
if headers:
for key in headers:
curlheaders[key] = headers[key]
if ufile is None:
return requests.post(url, data=json.dumps(data), headers=curlheaders)
else:
return requests.post(url, data=data, headers=curlheaders,files=ufile)
service.py
def processUpload(request):
data['company_country'] = request.POST.get('company_country')
data['company_name'] = request.POST.get('company_name')
#upload file
data['kyc_doc'] = request.FILES['kyc_doc'].file.getvalue()
files = {"kyc_doc":request.FILES['kyc_doc'].file.getvalue()}
return service.doCurl(data,files)
def doCurl(data,files):
#.....
# business logic here
#
del data['common_user_token']
response_kcloud = curl.post('https://app-sh.exmp.com/api/cret',data,{ "api-key":acc.api_key},files)
def post(url,data,headers,ufile=None):
curlheaders = {"Content-Type": "application/json"}
#empty data
if not data:
raise Exception("data cannot be empty")
#empty url
if not url:
raise Exception("Url cannot be empty")
#base case for header
if headers:
for key in headers:
curlheaders[key] = headers[key]
if ufile is None:
return requests.post(url, data=json.dumps(data), headers=curlheaders)
else:
return requests.post(url, data=data, headers=curlheaders,files=ufile)
curl.py
def processUpload(request):
data['company_country'] = request.POST.get('company_country')
data['company_name'] = request.POST.get('company_name')
#upload file
data['kyc_doc'] = request.FILES['kyc_doc'].file.getvalue()
files = {"kyc_doc":request.FILES['kyc_doc'].file.getvalue()}
return service.doCurl(data,files)
def doCurl(data,files):
#.....
# business logic here
#
del data['common_user_token']
response_kcloud = curl.post('https://app-sh.exmp.com/api/cret',data,{ "api-key":acc.api_key},files)
def post(url,data,headers,ufile=None):
curlheaders = {"Content-Type": "application/json"}
#empty data
if not data:
raise Exception("data cannot be empty")
#empty url
if not url:
raise Exception("Url cannot be empty")
#base case for header
if headers:
for key in headers:
curlheaders[key] = headers[key]
if ufile is None:
return requests.post(url, data=json.dumps(data), headers=curlheaders)
else:
return requests.post(url, data=data, headers=curlheaders,files=ufile)
您可以在上将
curl
转换为python代码您可以在上将curl
转换为python代码