Closed musnows closed 5 months ago
Hi @musnows , thank you for reaching out here!
更新文章时传入的 Body 有问题,可以试着更新这行代码:https://github.com/musnows/hexo-to-halo2/blob/a8b29459e342154211c8d404a287745957b34bd9/utils/Halo2Uploader.py#L96
- json=data["post"],
+ json=data,
目前尚不清楚 VSCode 插件中是否调用正确,需要 @ruibaby 确认一下。
建议提供请求的 payload,以及异常信息,当前无法判断。
目前尚不清楚 VSCode 插件中是否调用正确
没有问题,和 Console 一致。
另外还有一个问题,个人认为tags和categories接口在创建新的tag和分组的时候应该检查名字,如果名字已存在应该返回已有信息而不是新建一个tag或分组。
当前 Extension 生成的 API 不支持校验其他字段的唯一性,建议在创建之前先获取所有分类或者标签并匹配,没有匹配到才创建。
Hi @musnows , thank you for reaching out here!
更新文章时传入的 Body 有问题,可以试着更新这行代码:https://github.com/musnows/hexo-to-halo2/blob/a8b29459e342154211c8d404a287745957b34bd9/utils/Halo2Uploader.py#L96
- json=data["post"], + json=data,
目前尚不清楚 VSCode 插件中是否调用正确,需要 @ruibaby 确认一下。
已尝试去掉post,依旧是内部错误。
@ruibaby 请求的data完整载荷如下
{
"post": {
"spec": {
"title": "修改后的标题",
"slug": "971003431",
"template": "",
"cover": "https://img.musnow.top/i/2023/04/64302610057ae.png",
"deleted": false,
"publish": false,
"publishTime": "2023-08-02T07:52:14Z",
"pinned": false,
"allowComment": true,
"visible": "PUBLIC",
"priority": 0,
"excerpt": {
"autoGenerate": true,
"raw": ""
},
"categories": [
"category-wsAUf"
],
"tags": [
"tag-gfXTb",
"tag-wqREq"
],
"htmlMetas": []
},
"apiVersion": "content.halo.run/v1alpha1",
"kind": "Post",
"metadata": {
"name": "971003431"
}
},
"content": {
"raw": "test md content\n# 这是一个测试\n内容如下",
"content": "<p>test md content</p>\n<h1 id=\"_1\">这是一个测试</h1>\n<p>内容如下</p>",
"rawType": "markdown"
}
}
下面是两个API的调用结果。
调用 https://halo.musnow.top/apis/api.console.halo.run/v1alpha1/posts/971003431
的结果:
{"type": "about:blank", "title": "Internal Server Error", "status": 500, "detail": "Something went wrong, please try again later.", "instance": "https://halo.musnow.top/apis/api.console.halo.run/v1alpha1/posts/971003431", "requestId": "a572f7df-484", "timestamp": "2024-01-31T10:58:03.647872446Z"}
调用 https://halo.musnow.top/apis/api.console.halo.run/v1alpha1/posts/971003431/content
的结果:
{"spec": {"title": "【MySQL】deepin安装mysql的cpp开发包", "slug": "971003431", "releaseSnapshot": "b62881a3-887a-4344-ba09-6847c139f3e5", "headSnapshot": "c912e6b8-567a-4d1c-9687-db9267467efc", "baseSnapshot": "b62881a3-887a-4344-ba09-6847c139f3e5", "owner": "musnow", "template": "", "cover": "https://img.musnow.top/i/2023/04/64302610057ae.png", "deleted": false, "publish": true, "publishTime": "2023-08-02T07:52:14Z", "pinned": false, "allowComment": true, "visible": "PUBLIC", "priority": 0, "excerpt": {"autoGenerate": true, "raw": ""}, "categories": ["category-wsAUf"], "tags": ["tag-gfXTb", "tag-wqREq"], "htmlMetas": []}, "status": {"phase": "PUBLISHED", "conditions": [{"type": "PUBLISHED", "status": "TRUE", "lastTransitionTime": "2024-01-31T03:32:38.179473539Z", "message": "Post published successfully.", "reason": "Published"}, {"type": "DRAFT", "status": "TRUE", "lastTransitionTime": "2024-01-31T03:31:45.939853628Z", "message": "Drafted post successfully.", "reason": "DraftedSuccessfully"}], "permalink": "/posts/971003431", "excerpt": "在deepin下安装好mysql后,发现在c语言中没有<mysql.h>的头文件。 而根据ubuntu的办法直接按照mysql的开发包,会出现这种情况: ~/Desktop$ sudo apt-get install libmysqlclient-dev\n正在读取软件包列表… 完成\n正在分析软件包的", "inProgress": true, "contributors": ["musnow"], "lastModifyTime": "2024-01-31T03:32:37.370639323Z"}, "apiVersion": "content.halo.run/v1alpha1", "kind": "Post", "metadata": {"finalizers": ["post-protection"], "name": "971003431", "labels": {"content.halo.run/archive-year": "2023", "content.halo.run/archive-day": "02", "content.halo.run/archive-month": "08", "content.halo.run/published": "true", "content.halo.run/deleted": "false", "content.halo.run/owner": "musnow", "content.halo.run/visible": "PUBLIC"}, "annotations": {"content.halo.run/permalink-pattern": "/posts/{slug}", "checksum/config": "cf13720ac1ccd66900aac1faa743ec3884b96fea5795c694f7b5c539afb10d24", "content.halo.run/last-released-snapshot": "b62881a3-887a-4344-ba09-6847c139f3e5", "content.halo.run/preferred-editor": "vditor-mde"}, "version": 14, "creationTimestamp": "2024-01-31T03:31:45.908341035Z"}}
根据失败的requestId a572f7df-484
在halo2日志中定位到如下报错(请求时传入的是整个data)
如果依照vscode插件,只传入data["posts"]
部分,异常信息会不同。
我又重新看了看vscode插件的逻辑 https://github.com/halo-sigs/vscode-extension-halo/blob/3f2845b4a1e5b45b4dc0d2f227aada4604fe324b/src/service/index.ts#L40-L57 似乎如果需要更新文章,是根据name重新获取一个完整的posts信息再去PUT请求接口的,我的data载荷中post部分是否缺少字段?
在我这边尝试请求这两个接口,其中 /apis/api.console.halo.run/v1alpha1/posts/971003431/head-content 接口有内容,但是/apis/api.console.halo.run/v1alpha1/posts/971003431 会404
是根据name重新获取一个完整的posts信息再去PUT请求接口的,我的data载荷中post部分是否缺少字段?
如果是更新文章,建议先根据 metadata.name 获取完整的文章信息,然后再更新,因为服务内部在创建文章之后还有可能会去更新或者设置其他字段。
是 /apis/content.halo.run/v1alpha1/posts/971003431
,不是 /apis/api.console.halo.run/v1alpha1/posts/971003431
是根据name重新获取一个完整的posts信息再去PUT请求接口的,我的data载荷中post部分是否缺少字段?
如果是更新文章,建议先根据 metadata.name 获取完整的文章信息,然后再更新,因为服务内部在创建文章之后还有可能会去更新或者设置其他字段。
是
/apis/content.halo.run/v1alpha1/posts/971003431
,不是/apis/api.console.halo.run/v1alpha1/posts/971003431
感谢大佬指正!!!!我发现我update函数里面的posts_url的api链接也写错了😭我实在是太不细心了,向诸位大佬道歉。
现在我修改了代码,data['post']
和data['content']
部分都是直接调用/apis/content.halo.run/v1alpha1/posts/971003431和/apis/api.console.halo.run/v1alpha1/posts/971003431/head-content接口返回的内容。
下面的代码中,data['post']
部分只修改了文章的['spec']['title']
,此时PUT请求/apis/content.halo.run/v1alpha1/posts/971003431
成功。
# 文章id
slug = "971003431"
posts_url = f"/apis/content.halo.run/v1alpha1/posts/{slug}"
# 获取文章post的内容
data = {}
data["post"] = requests.get(url=BASE_URL + posts_url,
headers=BASE_HEADER).json()
# 修改文章标题
data["post"]["spec"]["title"] = "这是一个用于更新测试的新标题"
# 调用更新api
ret1 = requests.put(url=BASE_URL + posts_url,
json=data["post"],
headers=BASE_HEADER) # 这里要用json来传data
print(json.dumps(ret1.json(), ensure_ascii=False))
API请求结果如下
{"spec": {"title": "这是一个用于更新测试的新标题", "slug": "971003431", "releaseSnapshot": "b62881a3-887a-4344-ba09-6847c139f3e5", "headSnapshot": "c912e6b8-567a-4d1c-9687-db9267467efc", "baseSnapshot": "b62881a3-887a-4344-ba09-6847c139f3e5", "owner": "musnow", "template": "", "cover": "https://img.musnow.top/i/2023/04/64302610057ae.png", "deleted": false, "publish": true, "publishTime": "2023-08-02T07:52:14Z", "pinned": false, "allowComment": true, "visible": "PUBLIC", "priority": 0, "excerpt": {"autoGenerate": true, "raw": ""}, "categories": ["category-wsAUf"], "tags": ["tag-gfXTb", "tag-wqREq"], "htmlMetas": []}, "status": {"phase": "PUBLISHED", "conditions": [{"type": "PUBLISHED", "status": "TRUE", "lastTransitionTime": "2024-01-31T03:32:38.179473539Z", "message": "Post published successfully.", "reason": "Published"}, {"type": "DRAFT", "status": "TRUE", "lastTransitionTime": "2024-01-31T03:31:45.939853628Z", "message": "Drafted post successfully.", "reason": "DraftedSuccessfully"}], "permalink": "/posts/971003431", "excerpt": "在deepin下安装好mysql后,发现在c语言中没 有<mysql.h>的头文件。 而根据ubuntu的办法直接按照mysql的开发包,会出现这种情况: ~/Desktop$ sudo apt-get install libmysqlclient-dev\n正在读取软件包列表… 完成\n正在分析软件包的", "inProgress": true, "contributors": ["musnow"], "lastModifyTime": "2024-01-31T03:32:37.370639323Z"}, "apiVersion": "content.halo.run/v1alpha1", "kind": "Post", "metadata": {"finalizers": ["post-protection"], "name": "971003431", "labels": {"content.halo.run/archive-year": "2023", "content.halo.run/archive-day": "02", "content.halo.run/archive-month": "08", "content.halo.run/published": "true", "content.halo.run/deleted": "false", "content.halo.run/owner": "musnow", "content.halo.run/visible": "PUBLIC"}, "annotations": {"content.halo.run/permalink-pattern": "/posts/{slug}", "checksum/config": "cf13720ac1ccd66900aac1faa743ec3884b96fea5795c694f7b5c539afb10d24", "content.halo.run/last-released-snapshot": "b62881a3-887a-4344-ba09-6847c139f3e5", "content.halo.run/preferred-editor": "vditor-mde"}, "version": 18, "creationTimestamp": "2024-01-31T03:31:45.908341035Z"}}
标题成功更新
在这里记录一个自己遇到的小问题,即应该GET请求后立马在代码中用GET请求的返回值,修改相关信息后PUT,不应该用历史GET结果来模拟请求,否则会导致后台报错:
org.springframework.dao.OptimisticLockingFailureException: Failed to update table [extensions]; Version does not match for row with Id [/registry/content.halo.run/posts/971003431]
再次感谢大佬们的帮助,为自己的粗心看错API地址道歉。
系统信息
使用的哪种方式运行?
Docker
在线站点地址
https://halo.musnow.top/
发生了什么?
PUT调用更新文章信息失败
如题,我根据vscode插件的更新api调用逻辑尝试调用,文章内容可以被更新,但是文章信息调用失败
参考的vscode插件代码如下
我的python代码如下,详见 https://github.com/musnows/hexo-to-halo2/blob/a8b29459e342154211c8d404a287745957b34bd9/utils/Halo2Uploader.py#L33-L104
调用该函数时,第一个API调用失败,第二个content内容API调用成功
第一个API返回错误信息如下,第二个API返回的是正常结果。
可以观察到的结果是文章的内容在编辑器中已经被正常修改,但是文章的标题信息没有变化。因为还没有调用publish接口,所以网站展示的文章内容也没有变化。
分组和tag重名问题
另外还有一个问题,个人认为tags和categories接口在创建新的tag和分组的时候应该检查名字,如果名字已存在应该返回已有信息而不是新建一个tag或分组。
相关日志输出
附加信息
No response