正在尝试通过接口创建文档,发现categories字段无法指定已有分类。我看接口文档里面是写分类名称,但是不生效。这个字段正确应该如何写。
请求函数:
def create_doc(docx_path, config):
new_uuid = str(uuid.uuid4())
url = f"{config['base_url']}/apis/api.console.halo.run/v1alpha1/posts"
html_content = docx_to_html(docx_path, config)
print(html_content)
file_title = os.path.splitext(os.path.basename(docx_path))[0]
payload = {
"post": {
"spec": {
"title": file_title,
"slug": file_title,
"deleted": False,
"publish": True,
"pinned": False,
"allowComment": True,
"visible": "PUBLIC",
"priority": 0,
"excerpt": {
"autoGenerate": True,
"raw": ""
},
"categories": ["jumpserver"],
"tags": ["jumpserver"],
"htmlMetas": []
},
"apiVersion": "content.halo.run/v1alpha1",
"kind": "Post",
"metadata": {
"name": new_uuid,
"annotations": {"content.halo.run/preferred-editor": "default"}
}
},
"content": {
"raw": html_content,
"content": html_content,
"rawType": "HTML"
}
}
headers = config['headers']
try:
response = requests.request("POST", url, json=payload, headers=headers)
print(f"Status: {response.status_code}")
print(f"Post UUID: {new_uuid}")
try:
data = response.json()
print(data)
except Exception:
print("Response Text:", response.text)
except Exception as e:
print(f"请求失败: {e}")`
返回值:
`{
'spec': {
'title': '修改容器jms_net网段的MTU值',
'slug': '修改容器jms_net网段的MTU值',
'releaseSnapshot': 'f4a183f2-3a0d-4de2-93a9-e0dcb3ce56c9',
'headSnapshot': 'f4a183f2-3a0d-4de2-93a9-e0dcb3ce56c9',
'baseSnapshot': 'f4a183f2-3a0d-4de2-93a9-e0dcb3ce56c9',
'owner': 'root',
'deleted': False,
'publish': True,
'pinned': False,
'allowComment': True,
'visible': 'PUBLIC',
'priority': 0,
'excerpt': {
'autoGenerate': True,
'raw': ''
},
'categories': [
'jumpserver'
],
'tags': [
'jumpserver'
],
'htmlMetas': [
]
},
'status': {
'phase': 'DRAFT',
'conditions': [
{
'type': 'DRAFT',
'status': 'TRUE',
'lastTransitionTime': '2025-07-11T03: 21: 47.453329316Z',
'message': 'Draftedpostsuccessfully.',
'reason': 'DraftedSuccessfully'
}
]
},
'apiVersion': 'content.halo.run/v1alpha1',
'kind': 'Post',
'metadata': {
'name': 'e7e75afa-a18f-4ec1-a982-b0ebf271c223',
'annotations': {
'content.halo.run/preferred-editor': 'default'
},
'version': 1,
'creationTimestamp': '2025-07-11T03: 21: 47.443392801Z'
}
}
提前感谢各位大佬