【问题标题】:Python POST Request with an Image带有图像的 Python POST 请求
【发布时间】:2021-12-08 09:48:21
【问题描述】:

我正在尝试使用 python 向我的烧瓶网络服务器请求图像,但无法正常工作。

使用 cURL 很简单:

curl -XPOST -F "file=@image.jpg" http://127.0.0.1:5001

但是在 python 中使用我的代码:

import requests

with open("image.jpg", "rb") as a_file:
  file_dict = {"image.jpg": a_file}
  response = requests.post("http://127.0.0.1:5001", files=file_dict)
  print(response.text)
  print(response.status_code)

我只是获取返回的站点的 HTML 和状态 200。不是使用 cURL 返回的 JSON(并且是我想要返回的)。

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: python flask python-requests


    【解决方案1】:

    您可以使用以下代码

    files = {'file': open('image.jpg', 'rb')}
     
    r = requests.post('http://127.0.0.1:5001', files=files)
    print(r.text)
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2017-03-07
    • 2020-09-08
    • 2016-12-06
    相关资源
    最近更新 更多