GridDB Cloud调用Web API创建Container时出现Invalid Container Definition错误
解决GridDB Cloud Web API创建Container时的Invalid Container Definition错误(400 Bad Request)
核心问题
你的代码里存在一个极易忽略的语法错误:container_name键使用了中文双引号(“container_name”),而JSON规范要求所有键名和字符串必须使用英文双引号("container_name")。这个错误直接导致GridDB API无法正确解析容器定义,触发Invalid Container Definition报错。
修正后的代码
把中文引号替换为英文引号后,代码就能正常工作:
import requests import json # Replace with your actual username and password username = "xxxxxxx" password = "xxxxxxx" url = "http://127.0.0.1:8081/griddb/v2/mycluster/dbs/public/containers" headers = { "Content-Type": "application/json" } container_definition = { "container_name": "employees", "container_type": "COLLECTION", "row_key": True, "columns": [ {"name": "id", "type": "INTEGER"}, {"name": "name", "type": "STRING"}, {"name": "salary", "type": "DOUBLE"}, {"name": "join_date", "type": "TIMESTAMP"} ] } # Use the auth parameter to send Basic Auth credentials response = requests.post(url, headers=headers, json=container_definition, auth=(username, password)) if response.status_code == 201: print("Container created") else: print(f"Error creating container: {response.text}")
额外检查项(如果修正后仍报错)
- 确认
container_type的值大小写正确,GridDB Web API支持COLLECTION和TIME_SERIES两种类型,必须全大写 row_key设为True时,需确保主键列(这里是id)的类型合法,INTEGER类型符合要求- 验证API URL中的集群名
mycluster、数据库名public是否与你的GridDB配置一致 - 检查用户名和密码的正确性,确保拥有创建容器的权限
内容的提问来源于stack exchange,提问作者Jessica Williams
相关产品推荐
相关产品推荐

