Add_file_to_column API

Hey guys,

I am trying to build up a query to upload a file into a certain column. However I am having trouble.

Any help is highly appreciated.

ERROR:app:File upload failed: {“status_code”:400,“error_message”:“Unsupported query”,“errors”:[{“message”:“Unsupported query”,“extensions”:{“status_code”:400}}]}

if file and allowed_file(file.filename):
                
                filename = secure_filename(file.filename)
                logger.info(f"Uploading file: {filename} to item: {item_id}")
                # Use the same API_KEY that's already defined at the top of the file
                try:
                    # Save file temporarily
                    temp_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'temp')
                    os.makedirs(temp_dir, exist_ok=True)
                    temp_path = os.path.join(temp_dir, filename)
                    file.save(temp_path)
                    
                    # First upload the file to Monday.com's file storage
                    headers = {"Authorization": API_KEY, 'API-version':'2023-10'}
                    url = "https://api.monday.com/v2/file"
                    mutation_query = f"""
                        mutation ($file: File!){{
                            add_file_to_column (
                                item_id: 7815005218,
                                column_id: "file_mkp3rd8p",
                                file: $file
                            ) {{
                                id
                            }},
                            "map": "{{\\"file\\":\\"variables.file\\"}}"
                        }}
                    """
                    files = {
                        'query': (None, mutation_query),
                        'variables': (None, '{"file": null}'),
                        'file': (filename, open(temp_path, 'rb'), 'multipart/form-data')
                    }
                    response = requests.post(url, headers=headers, files=files)                               
                    
                    ```

Hello Ricardo, It looks like you’re trying to upload a file to a specific column on a Monday.com item, but you’re getting a 400 Unsupported query error. This usually happens due to incorrect formatting of the multipart/form-data GraphQL mutation, especially when dealing with file uploads.

Here’s what’s likely wrong and how to fix it:


:fire: The Problem

Your mutation string is trying to include the "map" key inside the mutation block, which is not correct. The "map" key should be outside the GraphQL query. The correct structure for multipart/form-data GraphQL file uploads follows the GraphQL multipart request spec.


:white_check_mark: The Fix

Here’s the corrected version of your code snippet:

python

CopyEdit

if file and allowed_file(file.filename):
    filename = secure_filename(file.filename)
    logger.info(f"Uploading file: {filename} to item: {item_id}")
    try:
        temp_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'temp')
        os.makedirs(temp_dir, exist_ok=True)
        temp_path = os.path.join(temp_dir, filename)
        file.save(temp_path)

        headers = {"Authorization": API_KEY, 'API-Version': '2023-10'}
        url = "https://api.monday.com/v2/file"

        # Mutation query without "map"
        mutation_query = """
            mutation ($file: File!) {
                add_file_to_column (
                    item_id: 7815005218,
                    column_id: "file_mkp3rd8p",
                    file: $file
                ) {
                    id
                }
            }
        """

        # Follow the GraphQL multipart spec
        files = {
            'query': (None, mutation_query),
            'variables': (None, '{"file": null}'),
            'map': (None, '{"file": ["variables.file"]}'),
            'file': (filename, open(temp_path, 'rb'), 'application/octet-stream')
        }

        response = requests.post(url, headers=headers, files=files)
        logger.info(f"Response: {response.status_code}, {response.text}")

    except Exception as e:
        logger.error(f"Error uploading file: {e}")

:brain: Key Points:

  • The "map" and "variables" fields are part of the multipart/form-data, not inside the GraphQL query.
  • Ensure "file": ["variables.file"] in "map" properly maps the file to the file variable.
  • The file content itself goes under the 'file' key.