Platon Efimov
Platon Efimov

Reputation: 652

Can't upload image by POST method from url. Python

I can't send an image object in multipart/form-data format without writing and reading it on the disk. This method does not work:

    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 thinks that image is too small. But this method works:

    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')})

How can I upload image without writing it on the disk?

Upvotes: 2

Views: 263

Answers (1)

Platon Efimov
Platon Efimov

Reputation: 652

Working code:

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)})

Upvotes: 1

Related Questions