Python &引用;“讯息”:&引用;所需请求部分\';文件\';“不存在”;
我想用REST和python上传一个文件。我可以用邮递员做这件事。但是,当我从Postman获取Python代码并尝试使用requests模块自己执行它时,我得到以下错误。请帮忙Python &引用;“讯息”:&引用;所需请求部分\';文件\';“不存在”;,python,python-2.7,rest,file-upload,postman,Python,Python 2.7,Rest,File Upload,Postman,我想用REST和python上传一个文件。我可以用邮递员做这件事。但是,当我从Postman获取Python代码并尝试使用requests模块自己执行它时,我得到以下错误。请帮忙 import requests url = "https://url******" payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"Pat
import requests
url = "https://url******"
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition:
form-data; name=\"file\"; filename=\"Path to file"\r\n\r\n\r\n------
WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW",
'auth_token': auth_token,
'cache-control': "no-cache",
}
response = requests.request("POST", url, data=payload, headers=headers,
verify=False)
print(response.text)
>>> response.text
u'{"message":"Required request part \'file\' is not
present","detailedMessage":"
","errorCode":-1,"httpStatus":500,"moreInfo":""}'
由于获取了错误的数据,您发布到的服务器上似乎出现了错误。 我没有你发布到的url,所以我不知道正确的数据格式。
尽量清楚地阅读参考资料将对您有所帮助。由于数据错误,您发布到的服务器上似乎出现了错误。 我没有你发布到的url,所以我不知道正确的数据格式。
请仔细阅读参考资料,这将对您有所帮助。我在尝试为类似的请求生成python代码时遇到了同样的问题。我通过对python代码进行以下更改来修复它:
import requests
url = "https://url******"
files = { 'file': ('file.wav', open('/path/to/file.wav', "rb"), 'audio/wave') }
headers = { 'auth_token': auth_token }
response = requests.request("POST", url, files=files, headers=headers)
print(response.text)
此问题是由“内容类型”:“多部分/表单数据”;边界=----WebKitFormBoundary7MA4YWxkTrZu0gW'
标题上的设置。使用文件时
不再需要此设置
在我的例子中,该文件是一个wav文件,mime类型可以相应地更改或完全删除(对我来说很有用)。当我尝试为类似的请求生成python代码时,遇到了同样的问题。我通过对python代码进行以下更改来修复它:
import requests
url = "https://url******"
files = { 'file': ('file.wav', open('/path/to/file.wav', "rb"), 'audio/wave') }
headers = { 'auth_token': auth_token }
response = requests.request("POST", url, files=files, headers=headers)
print(response.text)
此问题是由“内容类型”:“多部分/表单数据”;边界=----WebKitFormBoundary7MA4YWxkTrZu0gW'
标题上的设置。使用文件时
不再需要此设置
在我的例子中,该文件是一个wav文件,mime类型可以相应地更改或完全删除(对我有效)。在使用请求上载文件时,我遇到了相同的问题 1。通过从标题中删除内容类型 2.不要对有效负载执行json.dump操作 将解决问题
import requests
file_path='/home/ubuntu/workspace/imagename.jpg'
file_name=os.path.basename(file_path)
namew, extension = os.path.splitext(file_name)
type_dict = {'.pdf': 'application/pdf',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.tiff': 'image/tiff', '.jpg': 'image/jpg'}
url = "https://dev.example.com/upload"
filetype = type_dict.get(extension, 'application/octet-stream')
payload={}
files=[
('file',(file_name,open(file_path,'rb'),filetype))
]
headers = {
'Authorization': 'Token',
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
我在使用请求上传文件时也遇到了同样的问题 1。通过从标题中删除内容类型 2.不要对有效负载执行json.dump操作 将解决问题
import requests
file_path='/home/ubuntu/workspace/imagename.jpg'
file_name=os.path.basename(file_path)
namew, extension = os.path.splitext(file_name)
type_dict = {'.pdf': 'application/pdf',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.tiff': 'image/tiff', '.jpg': 'image/jpg'}
url = "https://dev.example.com/upload"
filetype = type_dict.get(extension, 'application/octet-stream')
payload={}
files=[
('file',(file_name,open(file_path,'rb'),filetype))
]
headers = {
'Authorization': 'Token',
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
嗨,我可以上传使用邮递员使用该网址。所以,url应该不是问题。嗨,我可以使用邮递员使用那个url上传。所以,url应该不是问题。