按Notion官方文档调用API创建表格遇validation_error错误求助
使用Notion API创建表格时的validation_error解决办法
你遇到的问题是Notion API对表格块的结构要求比标题块更严格:表格块必须显式定义table.children字段,用来指定表格的行数据,哪怕是初始的空行也不能省略。
错误提示里的body.children[0].table.children should be defined已经明确指出了缺失的字段——你的请求只设置了table_width,但没有提供表格的行内容。
修正后的请求代码
headers = { 'Authorization': f"Bearer {api_key}", "accept": "application/json", 'Content-Type': 'application/json', 'Notion-Version': '2022-02-22' } data = {'children': [ { 'object': 'block', 'type': 'table', 'table': { 'table_width': 4, 'children': [ # 表格的第一行,包含4个对应宽度的单元格 { 'type': 'table_row', 'table_row': { 'cells': [ [{'type': 'text', 'text': {'content': '列1'}}], [{'type': 'text', 'text': {'content': '列2'}}], [{'type': 'text', 'text': {'content': '列3'}}], [{'type': 'text', 'text': {'content': '列4'}}] ] } } ] } } ]} response = requests.patch('https://api.notion.com/v1/blocks/d5e1064a-c6aa-49e2-9f25-4111feac84ca/children', headers=headers, json=data)
关键说明
table.children是table_row类型块的数组,每一项对应表格的一行- 每个
table_row的cells数组长度必须和table_width一致,空单元格可以用[]来占位(比如[]表示空单元格) - 标题块属于无嵌套结构的基础块,不需要子元素字段;但表格块是带嵌套结构的块,必须声明子元素才能通过API验证
内容的提问来源于stack exchange,提问作者Denis Polikarpov
相关产品推荐
相关产品推荐

