Sert-X
Sert-X

Reputation: 13

Upload file via REST API with Python/Discord.py, IPBoard

i have a problem with a code. i'd like to upload on my site with /upload command on discord the attachment through IPB Rest API. IPB Rest API documentation here: https://invisioncommunity.com/developers/rest-api?endpoint=downloads/files/POSTindex

My code is:

async def some_command(interaction: discord.Interaction, file: discord.Attachment):
    filename1 = file.filename
    response1 = requests.get(file)
    data = response1.content
    files = {
         filename1 : data
         }
    response = upload_file("prova", 30, 2, IPS4_API_KEY, "provaprov", files)
    print(files)
    print(response)

def upload_file(title, category, author_id, api_key, description, filesupload):
    url = f"https://mysite/api/downloads/files"
    params = {
        "key": api_key,
        "description": description,
        "title": title,
        "category": category,
        "author": author_id,
        "files": filesupload
    }
    response = requests.post(url, params=params)
    return response.json()````

but i receive this error code:

{'errorCode': '1L296/B', 'errorMessage': 'NO_FILES'}

How can i fix it?
Reguards

i look online but didn't find an answer.
Can you help me? Thanks

Upvotes: 0

Views: 265

Answers (1)

Hazzu
Hazzu

Reputation: 2155

According to ducomentation, you must pass the files parameter separately when using requests:

from io import BytesIO


async def some_command(interaction: discord.Interaction, file: discord.Attachment):
    data = await file.read()
    files = {
         file.filename: BytesIO(data)
    }
    response = upload_file("prova", 30, 2, IPS4_API_KEY, "provaprov", files)
    print(files)
    print(response)

def upload_file(title, category, author_id, api_key, description, filesupload):
    url = f"https://mysite/api/downloads/files"
    params = {
        "key": api_key,
        "description": description,
        "title": title,
        "category": category,
        "author": author_id,
    }
    response = requests.post(url, params=params, files=filesupload)
    return response.json()

Upvotes: 1

Related Questions