2

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.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
aforbes
  • 143
  • 8
  • Try this: https://stackoverflow.com/questions/49458850/setting-custom-field-values-on-a-card-with-the-trello-api – Nico Velazquez Jul 01 '20 at 05:33
  • Here is how to add a body with the request library. https://stackoverflow.com/questions/11832639/how-to-specify-python-requests-http-put-body – Nico Velazquez Jul 01 '20 at 05:36
  • Thanks Nico. Yeah I did see that article and tried it, but it seems to make no difference. idential error is generated – aforbes Jul 01 '20 at 05:40

3 Answers3

2

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!')```
  • Works like a charm. I'd also suggest adding `token` and `key` as arguments so that they can be retrieved with `environ.get()`. – Christian Dec 17 '20 at 21:13
0
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
  • Yeah this is identical to the code I used, but it still gives the same error as above (different depending on whether I call the value "value" or "text) – aforbes Jul 01 '20 at 05:44
  • As in, this is identical to one of the variations of the code I tried. I've tried it with dozens of little variations, as well as sending the request as "json=body", as well as "data=json.dumps(body)" and "json=json.dumps(body)". nothing seems to work – aforbes Jul 01 '20 at 05:47
  • also with and without a 'headers' parameter as per your link, it also doesn't make a difference. – aforbes Jul 01 '20 at 05:47
  • I think my next step is to try and do this in a completely different language, and see if it still gives an error. In which case it would mean it's some sort of encoding issue? – aforbes Jul 01 '20 at 05:48
  • Maybe you should add header to request declaring the body as json. – Nico Velazquez Jul 01 '20 at 05:50
  • No good. tried with headers and it's an identical error – aforbes Jul 01 '20 at 05:52
  • headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} response = requests.put(url, params=querystring, data=body, headers=headers) – aforbes Jul 01 '20 at 05:52
0
body=
{
  "value": {
    "text": "<string>",
    "checked": true,
    "date": "2018-03-13T16:00:00.000Z",
    "number": 2154
  }
}

try this body instead

also refers to here: https://developer.atlassian.com/cloud/trello/rest/#api-cards-idCard-customField-idCustomField-item-put

and click body parameters example

hnandarusdy
  • 412
  • 5
  • 19
  • Traceback (most recent call last): File "test_trello.py", line 158, in update_trello_card = trello_card_update_custom_field_path_auth (card_id, custom_field_id, custom_field_value) File "test_trello.py", line 123, in trello_card_update_custom_field_path_auth "checked": true, NameError: name 'true' is not defined – aforbes Jul 01 '20 at 05:56
  • the custom field is a text field, so it makes sense it would reject the checkbox value. And trello wasn't updated – aforbes Jul 01 '20 at 05:57