Cloudflare Images - Error uploading images programmatically with python

For Workes & Pages, what is the name of the domain?

Images

What is the error number?

5455

What is the error message?

cloudflare.APIStatusError: Error code: 415 - {‘result’: None, ‘success’: False, ‘errors’: [{‘code’: 5455, ‘message’: ‘Uploaded image must have image/jpeg, image/png, image/webp, image/gif or image/svg+xml content-type’}], ‘messages’: }

What is the issue or error you’re encountering

python SDK - cf.images.v1.create() throws an error in all cases

What steps have you taken to resolve the issue?

I am using the python SDK in GitHub - cloudflare/cloudflare-python: The official Python library for the Cloudflare API (v3.1.0) , with Python 3.12.5 on Windows 11.
Tried several variants of the code:

1 cf = Cloudflare(api_token = secrets[“apitoken”])
2 with open(image, “rb”) as imgfile:
3 res = cf.images.v1.create(account_id = secrets[“accid”], file = imgfile.read())

In documentation, “file” is of type “object”, which does not say much, so I tried file=imgfile, file=“E:/test/test_file.jpg” and “file=io.open(image, ‘rb’)”, with same result.

What are the steps to reproduce the issue?

Run a code like this in python > 3.7:

import yaml
from pathlib import Path
from cloudflare import Cloudflare

def loadSecrets():
with open(‘…\secrets\cloudflare.yml’, “r”, encoding=“utf-8”) as secretsfile:
secrets = yaml.safe_load(secretsfile)
return secrets[“cloudflare”]

if name == ‘main’:
secrets = loadSecrets()
image = Path(“E:/”).joinpath(“test”, “test_pict.jpg”)
cf = Cloudflare(api_token = secrets[“image”][“apitoken”])
with open(image, “rb”) as imgfile:
res = cf.images.v1.create(account_id = secrets[“accid”], file = imgfile.read())

I cover the issue here

Looks like a bug in cloudflare-python, since the multipart body in the post requests that it sends does not match the API description . I will check if I am allows to open a bug for this.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.