无法通过POST方法从网址上传图片。蟒蛇

时间:2017-09-14 00:06:25

标签: python python-3.x request

我不能以multipart / form-data格式发送图像对象而无需在磁盘上写入和读取它。此方法不起作用:

    response = requests.get(offer['picture'])
    if not response.ok:
        print('Error!', response)
        continue
    image = response.content
    response = requests.post(upload_url, files={'file': image})

API认为图像太小。但这种方法有效:

    response = requests.get(offer['picture'])
    if not response.ok:
        print('Error!', response)
        continue
    image = response.content
    with open('test.jpg', 'wb') as file:
        file.write(image)
    response = requests.post(upload_url, files={'file': open('test.jpg', 'rb')})

如何在不将图像写入磁盘的情况下上传图像?

1 个答案:

答案 0 :(得分:1)

工作代码:

response = requests.get(offer['picture'])
if not response.ok:
    print('Error!', response)
    continue
image = response.content
response = requests.post(upload_url, files={'file': ('image.jpg', image)})