我正在使用 python 编辑我的 Trello 卡片 - 具体来说,我正在尝试更新卡片上的自定义字段值。
我的Python脚本可以将字段的值设置为“”就好,但是当我尝试将该值设置为非空字符串时,它会给出错误:
'Invalid custom field item value.', 'error': 'ERROR'
我认为这可能与编码有关,但是在尝试将值字典编码为 json 并将请求 header 设置为“application/json”之后,两者均无济于事,我没有主意了。
谁能帮我解决这个问题吗?
代码:
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)
以下是文档中此函数的链接:https://developer.atlassian.com/cloud/trello/rest/#api-cards-idCard-customField-idCustomField-item-put
编辑 - 我尝试更改请求的嵌套字典部分:
"value": {'text': 'this is a new string'}
只是:
'text': 'this is a new string'
但是我收到了这个错误:
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)
我还尝试将请求编辑为:
'value': '这是一个新字符串'
但是我收到了这个错误:
updated trello card custom field. json response:
{'message': 'Invalid custom field item value.', 'error': 'ERROR'}
我还尝试将字段值作为“数据”kwarg 发送,而不是在“params”kwarg 中发送,但这没有什么区别。
最佳答案
我遇到了同样的问题,很高兴为您提供一个有效的解决方案(我在这个网站上的第一个答案😊)。
您的响应定义:response = requests.request("PUT", url, params=querystring)
您需要像这样修改它:
response = requests.request("PUT", url, json=querystring)
这是我的功能:
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!')```
关于python-3.x - Trello API/Python - 无法设置卡片的自定义字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62619747/