Python API: Error while add comment with files

Hi, I get an error while attaching a png with the comment using the Python API.

code:

url = f"{server}/api/projects/{project_name}/task/{task_ayon_id}/activities"
headers = {"Authorization": f"apiKey {api_key}"}

activities = {
    "activityType": "comment", 
    "body": content, 
    "files": ["D:/attachment/annot_version_80371.png"]
}
print("Checking note content:", activities)

# Push the comment to Ayon.
response = requests.post(url, headers=headers, json=activities)

Error:

Error: 500
('{"code":500,"detail":"Internal server error","traceback":"invalid input for '
 "query argument $1: ['D:/attachment/annot_version_80371.png'... (Invalid "
 'entity ID D:/attachment/annot_version_80371.png)","path":"[POST] '
 '/projects/PRPT/task/0e828bc0b31411efadef7a3a96401200/activities","file":"asyncpg/protocol/prepared_stmt.pyx","function":"asyncpg.protocol.protocol.PreparedStatementState._encode_bind_msg","line":204}')

When trying to add a comment without files, it is working fine.

Going based off of the error message I have the feeling the image needs to be uploaded separately (other post request) and instead the files here may need to be referring to the entity ids of those image assets then. But I have never tried this myself.

Likely @martin.wacker may have pointers here.

1 Like

@BigRoy Thank you, It works by uploading files separately and passing the file ID in the file list.

But the uploaded file is not viewable in the comment
image

I have downloaded the file and opened it; it is not open and got error file format not supported.

I opened the file in a text editor and removed the below lines in the top and bottom of the file and saved it. It has worked, and I can open the image now.

top lines:
–8aa3671c3fd2639125782361204f94b7
Content-Disposition: form-data; name=β€œfile”; filename=β€œfile”

bottom line:
–8aa3671c3fd2639125782361204f94b7–

Kindly suggest what went wrong. Thanks in Advance

code used to upload file

mime_type, _ = mimetypes.guess_type(file_path)
url = f"{server}/api/projects/{project_name}/files"
headers = {"Authorization": f"apiKey {api_key}", "Content-Type": mime_type, "X-File-Name": note["attachments"][0]["name"]}
files = {"file": open(file_path, "rb")}
response = requests.post(url, headers=headers, files=files)

It looks like you are uploading the file as multipart. AYON supports only one file and metadata are provided in the request headers

with open(PATH, "rb") as f:
    requests.post(
        f"{BASE_URL}/api/projects/{PROJECT_NAME}/files",
        data=f.read(),
        headers={
            "Content-Type": "video/mp4",
            "x-file-name": os.path.basename(PATH),
            "x-api-key": API_KEY
        },
    )
2 Likes

Hi @martin.wacker Thank you; it is working well.

2 posts were split to a new topic: Python API: Line breaks in creating new comments

Just want to share the equivalents of this using ayon_api as came up on Discord here.

Recommended: using ayon_api.upload_file

import ayon_api
import os
import mimetypes

project_name = "project_name"
filepath = "e:/testfile.jpg"
mime_type, _ = mimetypes.guess_type(filepath)

headers = {
    "Content-Type": mime_type,
    "x-file-name": os.path.basename(filepath),
}
response = ayon_api.upload_file(
    f"projects/{project_name}/files", 
    filepath, 
    request_type=ayon_api.RequestTypes.post,
    headers=headers)

entity_id = response.json().get("id")

Using ayon_api.raw_post

import ayon_api
import os
import mimetypes

project_name = "project_name"
filepath = "e:/testfile.jpg"
mime_type, _ = mimetypes.guess_type(filepath)

with open(filepath, "rb") as f:
    payload = {
        "data": f.read(),
        "headers": {
            "Content-Type": mime_type,
            "x-file-name": os.path.basename(filepath),
        },
    }
    response = ayon_api.raw_post(f"projects/{project_name}/files", **payload)

entity_id = response.get("id")
2 Likes