Trello API / Python - Can't Set Custom Field Value For Card

Viewed 830

I'm using python to edit my Trello cards - specifically, I'm trying to update Custom Field values on the cards.

My python script can set the value of the field to "" just fine, but when I try to set the value to a non-empty-string, it gives an error:

'Invalid custom field item value.', 'error': 'ERROR'

I think it might be something to do with encoding, but after trying to encode the value dict to json, and set the requests header to "application/json", both to no avail, I'm out of ideas.

Can anyone help me with this?

Code:

def trello_card_update_custom_field(card_id, custom_field_id, custom_field_value):
    url = "https://api.trello.com/1/cards/{card_id}/customField/{custom_field_id}/item".format(card_id=card_id, custom_field_id=custom_field_id)
    querystring = {
        "key": key,
        "token": token,
        "value": {'text': 'this is a new string'}
        #"value": ""
    }
    response = requests.request("PUT", url, params=querystring)
    response_json = response.json()
    return response_json
# end function


update_trello_card = trello_card_update_custom_field(card_id, custom_field_id, custom_field_value)
print("updated trello card. json response:\n", update_trello_card)

Here's the link to this function in the documentation: https://developer.atlassian.com/cloud/trello/rest/#api-cards-idCard-customField-idCustomField-item-put

edit - I tried changing the nested dict part of the request:

"value": {'text': 'this is a new string'}

to just:

'text': 'this is a new string'

But I got this error:

Traceback (most recent call last):
  File "test_trello.py", line 153, in <module>
    update_trello_card = trello_card_update_custom_field_path_auth (card_id, custom_field_id, custom_field_value)
  File "test_trello.py", line 129, in trello_card_update_custom_field_path_auth
    response_json = response.json()
  File "C:\Users\andre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I also tried editing the request to:

'value': 'this is a new string'

But I got this error:

updated trello card custom field. json response:
 {'message': 'Invalid custom field item value.', 'error': 'ERROR'}

I also tried sending the field value as a 'data' kwarg, rather than in the 'params' kwarg, but it made no difference.

3 Answers

I have had the same issue and am happy to present you with a working solution (my first ever answer on this site ).

Your response defintion: response = requests.request("PUT", url, params=querystring)

You need to amend it like so: response = requests.request("PUT", url, json=querystring)

Here is my function:

def update_custom_field(card_id,custom_field_id,value_type,value):
   url = f'https://api.trello.com/1/card/{card_id}/customField/{custom_field_id}/item'
   payload = {'token':token,'key':key,'value':{value_type: value}}
   request = requests.put(url,json=payload) 
   return request.text

update_custom_field(card_id,custom_field_id,'text','hello word!')```
def trello_card_update_custom_field(card_id, custom_field_id, custom_field_value):
url = "https://api.trello.com/1/cards/{card_id}/customField/{custom_field_id}/item".format(card_id=card_id, custom_field_id=custom_field_id)
querystring = {
    "key": key,
    "token": token,
}
body = {'text': 'this is a new string'}  
response = requests.request("PUT", url, data=body, params=querystring)
response_json = response.json()
return response_json
Related