通过 API PUT 请求更新帖子

为了确认理解,Discourse API 文档提到了这种结构…

{
"post[raw]": "string",
"post[raw_old]": "string",
"post[edit_reason]": "string",
"post[cooked]": "string"
}

在我尝试之前,这是否意味着,或者说,这具体是什么意思?

是指 {post: {raw: ..., raw_old: ... }} 吗?

还是仅仅指 {raw: "", raw_old: ""...}

另外,raw_old 是什么?文档仅说明每个元素都是我要 PUT 到 JSON 主体中的“字符串”。

我只是想在帖子顶部添加一行(该帖子最初是由一个机器人创建的),我们需要在其中添加一个指向另一个网站的链接。注意,我们的目标是将第一段显示在分类标题下方,或者在 cooked

元素中显示(该元素定义在分类标题正下方)。

希望这能说明清楚。如果不清楚,请让我澄清。

I’m not actually sure what raw_old is for at the moment, but you just need to use the raw field and it will replace the entire post. So, in your code you would fetch the post you want to edit, add your line to the top, then update the post using the raw field.

It might be helpful to see how Discourse does it by editing a post live on your site and inspecting the browser calls:

This is also how the discourse_api gem does it:

Just a question on this, are you referring to different documentation then what is defined at Discourse API Docs ? That would be interesting… however I am only going by the API docs.

My simple goal is to simply add a comment + link below the category name, which I think Discourse takes from the first Post of the "About … " topic? Like I’m just looking for what it will insert into the

… element or what I need to do to make my post appear there. Does my explanation make sense?

From the above, and from attempting, you can not update a post using “cooked” as the API documentation states, it is however possible with “raw”, all attempts I made using “cooked” kept giving me back an error that my post was invalid as it was under 20 characters, or not a clear sentence …

However there are additional issues with “raw” that I will make into another post as this isn’t directly related.

Thanks for all your help.

There is a slight discrepancy in API Docs:

Documentation says: PUT /posts/{id}.json with payload

Content type: application/json

{
  "post[raw]": "string",
  "post[raw_old]": "string",
  "post[edit_reason]": "string",
  "post[cooked]": "string"
}

but this only works with PUT /posts/{id} and Content-Type: application/x-www-form-urlencoded.

The correct payload for PUT /posts/{id}.json should be:

Content type: application/json

{
  "post": {
      "raw": "string",
      "raw_old": "string",
      "edit_reason": "string",
      "cooked": "string"
    }
}

The raw parameter is sufficient for replacing the entire post.

是的,谢谢。我在尝试了几次 Postman 请求并自行测试后意识到了这一点……此外,在我的尝试中,“cooked” 似乎没有任何作用……而且我仍然不确定 “raw_old” 是什么。

今天有人给我分配了这个任务。也许可以取消分配 :slight_smile:

无论如何,文档现在是正确的:https://docs.discourse.org/#tag/Posts/operation/updatePost