Upload file using Python and getting 'Unsupported query' error

I’m trying to upload an image to a monday update using the add_file_to_update mutation.

I’ve referenced the following posts:

With the help of those posts, I have the following code:
My code

import requests
import os

def add_file_to_update(update_id):
    api_key = os.environ.get("MONDAY_API_KEY")
    headers = {"Authorization" : api_key}
    apiUrl = "https://api.monday.com/v2/file"

    mutate_query = 'mutation ($updateId: Int!, $file: File!) { add_file_to_update (update_id: $updateId, file: $file) { id } }'
    vars = {
        'updateId' : int(update_id),
    }

    add_file = {'query' : mutate_query, 'variables' : vars}
    dir_path = os.path.dirname(os.path.realpath(__file__))
    files = [('variables[file]',('image.png',open(dir_path+'/image.png','rb'),'image/png'))]
    r = requests.post(url=apiUrl, json=add_file, files=files, headers=headers)

The image.png file is a file that has no issues, as in I am able to open and view the file.

Unfortunately, I’m getting this error message, and I’m not sure how to debug the error.

Error message

{'error_message': 'Unsupported query', 'status_code': 400}

Does anyone have any idea where/what the issue is?

I’ve tried to send this request with Postman, copying the method used by @AlexSavchuk here:

mutation add_file($update_id:Int!, $file: File!) {add_file_to_update(update_id: $update_id, file:$file) {id}}

image

But when I try, I’m getting a 500 Internal Server Error*:

*I get this error for both the https://api.monday.com/v2/ and https://api.monday.com/v2/file endpoints.

Hi @chelseaw!

Do you mind sending your full mutation request? This way I can check for errors.

Thanks!
Helen

Hi @Helen

This is the cURL command generated from Postman:

curl --location --request POST 'https://api.monday.com/v2/file' \

--header 'Authorization: XXXX' \

--header 'Cookie: __cf_bm=dd17f964e4380b3f79205e08deaa980092c327c8-1624024759-1800-AZbhDH93kC+21Vo+sFmGdM5etK29Nt8QQcQQfk/8NoQXUzB2Gqx7dN7BHnrVkAlm5v1Y8OBTMJ7npI79qfxq0js=' \

--form 'query="mutation add_file($update_id:Int!, $file: File!) {add_file_to_update(update_id: $update_id, file:$file) {id}}"' \

--form 'variables="{“update_id”:1397157612}"' \

--form 'map=" {“image”:“variables.file”}"' \

--form 'image="/full/path/to/image.png

"'

I get the following error messages for the 2 endpoints:
Screen Shot 2021-06-18 at 10.20.14 AM

And here’s the python code again. I cleaned it up here so the variable inputs are clear:

apiUrl = "https://api.monday.com/v2"
api_key = "XXXXXXXX"
headers = {"Authorization" : api_key}
update_id = "1397157612"

def add_file_to_update(update_id):
    mutate_query = 'mutation ($updateId: Int!, $file: File!) { add_file_to_update (update_id: $updateId, file: $file) { id } }'
    vars = {
        'updateId' : int(update_id)
    }

    add_file = {'query' : mutate_query, 'variables' : vars}

    files = [('variables[file]',('image.png',open('/full/path/to/image.png','rb'), 'png'))]
    r = requests.post(url=apiUrl, json=add_file, files=files, headers=headers)
    r_json = r.json()
    print(r_json)

I get the following error messages for the 2 endpoints:

# https://api.monday.com/v2
{'errors': [{'message': 'No query string was present'}], 'account_id': XXXXXX}
# https://api.monday.com/v2/file
{'error_message': 'Unsupported query', 'status_code': 400}

Hi @chelseaw,

Hmm weird, I’m unable to spot any errors in the query that you’re sending. There are a few things that I think could be going on:

  1. Could there be file upload permissions for your account implemented by the admin of your account? I know there is a permissions setting that prevents users (all except admins) from uploading files into the account. A simple test would be: are you able to upload files in the UI, and are only experiencing trouble when uploading files with the API?

  2. Do you mind double-checking if the ID you’re using for your update is indeed the right one? To check, just copy the link for your update (see the screenshot below), and see if the last set of digits in the update URL are the ones you’re sending in your query:

  3. Does the issue persist for you for any other update? What about for updates on a separate board? What if you sent a different image file?

Thanks for your patience and assistance here! I want to make sure we cover all of our bases in case this is a bug.

Hi @Helen,

Thank you for taking the time to help me with this, I appreciate it.

  1. I am able to upload files in the UI. I am only experiencing trouble when uploading files with the API. But I will double-check with the team admin to see if there is a file upload permission that is not enabled for my account. See video below.

  2. I’ve double-checked and can confirm that I’m using the last set of digits in the update URL.

  3. The issue persists for other updates on (a) the same board and (b) a separate board. The issue persists if I send a different image. :confused:

Other details that may be helpful, I’m using the API token under Developer > My Access Tokens.

Other mutations have been working for me successfully thus far (create an item, update various columns of an item, create an update within an item) .

I now have working add_file_to_update and add_file_to_column queries.

See below for a number of fixes.

  1. I switched to using f-strings in Python for my mutation query. See below the updated python code:
    Add file to column
item_id = "1054445315"

def add_file_to_column(item_id):
    api_key = "XXXXXXX"
    headers = {"Authorization" : api_key}
    apiUrl = "https://api.monday.com/v2/file"
    q = f"""
            mutation add_file($file: File!) {{
                add_file_to_column (item_id: {int(item_id)},
                            column_id: files,
                            file: $file
                        ){{
                    id
                }}
            }}
        """

    dir_path = os.path.dirname(os.path.realpath(__file__))
    f = dir_path+'/image.png'

    files = {
        'query': (None, q, 'image/png'),
        'variables[file]': (f, open(f, 'rb'), 'multipart/form-data', {'Expires': '0'})
    }
    
    r = requests.post(url=apiUrl, files=files, headers=headers)
    r_json = r.json()
    print(r_json)

Add file to update

update_id = "13971576121"

def add_file_to_update(update_id):
    api_key = "XXXXXXX"
    headers = {"Authorization" : api_key}
    apiUrl = "https://api.monday.com/v2/file"
    q = f"""
            mutation add_file($file: File!) {{
                add_file_to_update (update_id: {int(update_id)},
                            file: $file
                        ){{
                    id
                }}
            }}
        """
    dir_path = os.path.dirname(os.path.realpath(__file__))
    f = dir_path+'/image.png'

    files = {
        'query': (None, q, 'image/png'),
        'variables[file]': (f, open(f, 'rb'), 'multipart/form-data', {'Expires': '0'})
    }

    r = requests.post(url=apiUrl, files=files, headers=headers) #
    r_json = r.json()
    print(r_json)
  1. I was not using the correct set of digits for the update_id. Thank you @Helen. I was using the item_id. For future reference: /pulses/item_id/posts/update_id. This led to a successful curl POST request:
curl --location --request POST 'https://api.monday.com/v2/file' \
--header 'Authorization: XXXXXXX' \
--form 'query="mutation add_file($file: File!, $update_id: Int!) {add_file_to_update(update_id: $update_id, file: $file) {id}}"' \
--form 'variables="{\"update_id\":1053334092}"' \
--form 'map="{\"image\":\"variables.file\"}"' \
--form 'image=@"/Users/chelsea/workdir/git-box/db-ops/slack-monday/image.png"'
  1. The team admins made me an admin for the account, in case it was the file permissions issue.
1 Like

I’m so glad to hear @chelseaw!

Congrats on resolving this!

Helen

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.