【发布时间】:2020-10-18 12:45:18
【问题描述】:
我正在使用 python 来编辑我的 Trello 卡片 - 具体来说,我正在尝试更新卡片上的自定义字段值。
我的 python 脚本可以将字段的值设置为 "" 就好了,但是当我尝试将值设置为非空字符串时,它给出了一个错误:
'自定义字段项值无效。', 'error': 'ERROR'
我认为这可能与编码有关,但是在尝试将值 dict 编码为 json 并将请求标头设置为“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
edit - 我尝试更改请求的嵌套 dict 部分:
"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'}
我也尝试将字段值作为“data”kwarg 发送,而不是在“params”kwarg 中发送,但没有任何区别。
【问题讨论】:
-
这里是如何使用请求库添加正文。 stackoverflow.com/questions/11832639/…
-
谢谢尼科。是的,我确实看过那篇文章并尝试过,但似乎没有任何区别。产生相同的错误
标签: python-3.x api trello