discord-api-docs icon indicating copy to clipboard operation
discord-api-docs copied to clipboard

Create message endpoint fails with Cloudflare 520

Open pam-param-pam opened this issue 10 months ago • 0 comments

Description

Create message endpoint fails with cloudflare's status code 520 when:

  • only files are supplied
  • the bigger the file size the more often it will fail
  • auth token provided is invalid

Steps to Reproduce


import httpx

size_mb = 7
size = size_mb * 1024 * 1024  # Convert MB to bytes

data = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
with open("random_file.txt", 'wb+') as file:
    while size > 0:
        chunk_size = min(1024 * 1024, size)
        file.write(data * (chunk_size // len(data)))
        size -= chunk_size

    channel_id = 1207210135194181685
    test_url = f"https://discord.com/api/v10/channels/{channel_id}/messages"
    with httpx.Client(timeout=None) as client:
        res = client.post(test_url, files={'file': file}, headers={"Authorization": "Bot invalidTokenHere"})
        print(res.status_code)

Weirdly, this only happens when using the httpx library—when I switch to requests, I get a 401 as expected.

This suggests that httpx and requests handle the request differently, but it also indicates a potential bug in the API

Expected Behavior

The API returns 401 status code.

Current Behavior

The API returns 520 status code.

Screenshots/Videos

No response

Client and System Information

Windows 10 python=3.9 httpx==0.26.0

pam-param-pam avatar Feb 26 '25 18:02 pam-param-pam