cloudflare / cloudflare/cloudflare-python

Create Image Upload is broken for Sync and Async

Đang mở
#2,569 3 bình luận 6 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
506
Fork
148
Merge trung bình
3 giờ 15 phút
Pull request đã merge (30 ngày)
1

Mô tả

### Confirm this is a Python library issue and not an underlying Cloudflare API issue.

- [x] This is an issue with the Python library

### Describe the bug

The Cloudflare Image Create function is broken.

There is an example below, this has also been discussed on StackOverflow

https://stackoverflow.com/questions/79032782/error-5455-when-uploading-to-cloudflare-images-using-python-library

I cant see any test coverage for this use case.

https://github.com/cloudflare/cloudflare-python/blob/fc6ca90779cd230dc06445ba42e4c540da186ce0/tests/api_resources/images/test_v1.py#L28

### To Reproduce

This is a **working** example using Python Requests
```python
import requests
with open(
"water bottle.webp", "rb"
) as f:
data = f.read()

# try requests
files = {
"file": (
"water bottle.webp",
data,
"image/webp",
)
}

response = requests.post(
url=f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/images/v1",
headers={
"Authorization": f"Bearer ..."
},
files=files,
)
print(response.status_code)
print(response.json())
```

This is a **broken** example using the Cloudflare Sync function

```python
with open(
"water bottle.webp", "rb"
) as f:
data = f.read()

from cloudflare import Cloudflare

client = Cloudflare(
api_token='...'
)

# Create a tuple: (filename, binary data, content type)
file_tuple = ("water bottle.webp", data, "image/webp",)

cloudflare_image = await client.images.v1.create(
account_id=CLOUDFLARE_ACCOUNT_ID,
file=file_tuple,
)
print(cloudflare_image)
```

The output is
```
cloudflare.BadRequestError: Error code: 400 - {'result': None, 'success': False, 'errors': [{'code': 5400, 'message': 'Bad request: Error parsing form fields'}], 'messages': []}
```

This is a broken example using the Async Cloudflare function

```python
from cloudflare import AsyncCloudflare

client = AsyncCloudflare(
api_token='...'
)

# Create a tuple: (filename, binary data, content type)
file_tuple = ("water bottle.webp", data, "image/webp",)

cloudflare_image = await client.images.v1.create(
account_id=CLOUDFLARE_ACCOUNT_ID,
file=file_tuple,
)
print(cloudflare_image)
```

The output is
```
cloudflare.BadRequestError: Error code: 400 - {'result': None, 'success': False, 'errors': [{'code': 5400, 'message': 'Bad request: Error parsing form fields'}], 'messages': []}
```

### Code snippets

```Python

```

### OS

macOS

### Python version

3.12.9

### Library version

v4.1.0

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.