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

如何在Python中实现cURL命令上传.csv文件到指定API的功能

Python实现cURL上传CSV文件方案

首先安装所需依赖库:
pip install requests

转换后的可执行代码如下:

import requests

# 替换为实际接口地址
url = "http://**************"
# 替换为实际API授权令牌
api_token = "**************"
# 替换为实际CSV文件本地存储路径
csv_file_path = "/C:/Users/1288956/Downloads/ozon_search_query_test.csv"

headers = {
    "Accept": "application/json",
    "API-AUTH-TOKEN": api_token
}

# 对应cURL的--form表单参数,list为表单字段名
files = {
    "list": open(csv_file_path, "rb")
}

response = requests.post(
    url=url,
    headers=headers,
    files=files,
    allow_redirects=True # 对应cURL的--location参数,requests默认开启可省略
)

# 校验返回结果
print(response.status_code)
print(response.json())

注意事项

  • 建议使用with上下文管理器打开文件,避免运行异常导致文件资源泄漏
  • 如果接口有指定文件MIME类型要求,可在files参数中补充对应配置

内容的提问来源于stack exchange,提问作者Sum Zbrod

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 01:24:05