小编spd*_*ilv的帖子

使用 Python 请求通过 POST 请求发送图像

我目前正在尝试使用 Python (3.5) 和 Requests 库来发送 POST 请求。此 POST 将发送一个图像文件。这是示例代码:

import requests

url = "https://api/address"

files = {'files': open('image.jpg', 'rb')}
headers = {
    'content-type': "multipart/form-data",
    'accept': "application/json",
    'apikey': "API0KEY0"
    }
response = requests.post(url, files=files, headers=headers)

print(response.text)
Run Code Online (Sandbox Code Playgroud)

但是,当我运行代码时,我收到 400 错误。我设法到达了 API 端点,但在响应中它指出我未能发送任何文件。

{
  "_id": "0000-0000-0000-0000", 
  "error": "Invalid request", 
  "error_description": "Service requires an image file.", 
  "processed_time": "Tue, 07 Nov 2017 15:28:45 GMT", 
  "request": {
    "files": null, 
    "options": {}, 
"tenantName": "name", 
"texts": []
  }, 
  "status": "FAILED", 
  "status_code": 400, 
  "tenantName": "name"
}
Run Code Online (Sandbox Code Playgroud)

“文件”字段显示为空,这对我来说似乎有点奇怪。POST 请求在 …

python post python-3.x python-requests postman

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

post ×1

postman ×1

python ×1

python-3.x ×1

python-requests ×1