【问题标题】:Issue while sending a file to an API将文件发送到 API 时出现问题
【发布时间】:2016-04-21 09:10:30
【问题描述】:

我正在尝试将文件发送到 API,然后得到响应 - CSV 文件 (我已经看到 different posts 关于它,但我无法让它工作)

文档中的示例使用httpie

http --timeout 600 -f POST http://api-adresse.data.gouv.fr/search/csv/ data@path/to/file.csv

但是当我使用请求时,我得到一个400 Bad Request

path = '/myfile.csv'
url = 'http://api-adresse.data.gouv.fr/search/csv/'
files = {'file': open(path, 'rb')}
res = requests.post(url, data=files)

【问题讨论】:

    标签: python api http-post python-requests


    【解决方案1】:

    You need to specify files keyword argument,而不是 data 来发布多部分/表单数据请求。

    并且密钥应该匹配:file -> data

    path = 'path/to/file.csv'
    url = 'http://api-adresse.data.gouv.fr/search/csv/'
    files = {'data': open(path, 'rb')}
    #        ^^^^^^
    res = requests.post(url, files=files)
    #                        ^^^^^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2018-10-24
      • 2021-02-27
      • 1970-01-01
      • 2020-12-26
      相关资源
      最近更新 更多