Python调用Zoho Files POST上传API报错‘未指定文件或文件为空’求助
Let's fix this issue step by step. The error "No File Specified or Empty File" pops up because of a few key mistakes in how you're structuring the request with the requests library, plus a URL formatting glitch.
What's Going Wrong
- Invalid URL Format: The
&in your URL is an HTML escaped character—servers can't parse this correctly for query parameters. You need to use a plain&instead. - Incorrect File Upload Structure: You're trying to pass the file object inside the
datadictionary'scontentfield, butrequestsrequires binary files to be sent via thefilesparameter. Shoving the file intodatabreaks the request format, so the server can't recognize your file content. - Mismatched Filenames: Your
fileNamevariable is set toa.xsl, but the filename in your file tuple isreport.xls. This inconsistency can confuse the server's file handling logic.
Fixed Code
import requests AuthToken = "xyz" fileName = "a.xls" # Keep filename consistent across the request FolderID = "xxxxxxxxx" file_path = "pathToFile" # Replace with your actual file path # Fix URL: replace & with plain & URL = f"https://apidocs.zoho.com/files/v1/upload?authtoken={AuthToken}&scope=docsapi" # Form data (non-file parameters) mydata = { 'filename': fileName, 'fid': FolderID } # File content mapped to the API's required "content" field # Use a with statement to safely handle file opening/closing with open(file_path, 'rb') as file: files = { 'content': (fileName, file, 'application/vnd.ms-excel') } r = requests.post(url=URL, data=mydata, files=files) print(r.status_code) print(r.text)
Key Changes Explained
- URL Fix: Swapped
&for&so the server correctly reads both theauthtokenandscopeparameters. - Separate Data & Files: Split form data (like
filenameandfid) into thedataparameter, and file content into thefilesparameter. This tellsrequeststo generate the propermultipart/form-datarequest format that the Zoho API expects. - Safe File Handling: Using
with open(...)ensures the file is closed automatically after the request, preventing resource leaks. - Consistent Filenames: Aligned the filename in the file tuple with the
filenameparameter to avoid server-side confusion.
内容的提问来源于stack exchange,提问作者Nawaf Momin
相关产品推荐
相关产品推荐

