You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. 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.
  2. Incorrect File Upload Structure: You're trying to pass the file object inside the data dictionary's content field, but requests requires binary files to be sent via the files parameter. Shoving the file into data breaks the request format, so the server can't recognize your file content.
  3. Mismatched Filenames: Your fileName variable is set to a.xsl, but the filename in your file tuple is report.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 the authtoken and scope parameters.
  • Separate Data & Files: Split form data (like filename and fid) into the data parameter, and file content into the files parameter. This tells requests to generate the proper multipart/form-data request 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 filename parameter to avoid server-side confusion.

内容的提问来源于stack exchange,提问作者Nawaf Momin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 16:59:05