# 使用API无法锁定帖子

**URL:** https://meta.discourse.org/t/unable-to-lock-post-with-api/228974
**Category:** Support
**Created:** [2022年六月3日 19:19 UTC](https://meta.discourse.org/t/unable-to-lock-post-with-api/228974 "2022-06-03T19:19:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mksafi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mksafi/32/244062_2.png) [@mksafi](https://meta.discourse.org/u/mksafi)
#### Post date: [2022年六月3日 19:19 UTC](https://meta.discourse.org/t/unable-to-lock-post-with-api/228974/1 "2022-06-03T19:19:28Z")

</div>

我从我的应用程序向 Discourse 发出了一些 API 调用，以修改帖子。

在第一次调用中，我编辑了帖子。这效果很好。在下一次调用中，我尝试锁定帖子。

我从 JavaScript 发出的帖子锁定调用如下所示：

```javascript
fetch(`https://forum.hollowverse.com/posts/1817/locked.json`, {
  method: 'PUT',
  headers: {
    'Api-Key': process.env.DISCOURSE_SYSTEM_PRIVILEGE_SECRET,
    'content-type': 'application/json',
    'Api-Username': 'hollowbot',
  },
  body: JSON.stringify({locked: true}),
});

```

当我发出此调用时，Discourse 以 `{locked: false}` 回复我。

我尝试手动锁定帖子并检查 Discourse 客户端发出的 API 调用，它与我从应用程序发出的调用非常相似，唯一的例外可能是身份验证方法。

不确定发生了什么，有什么想法吗？

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2022年六月3日 19:22 UTC](https://meta.discourse.org/t/unable-to-lock-post-with-api/228974/2 "2022-06-03T19:22:54Z")

</div>

我没有发现明显的问题。您是通过遵循[如何逆向工程 Discourse API](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576) 来获得这个结果的吗？如果我在电脑旁，我也会这样做来提供帮助。

---

<div class="post-metadata">

### Author: ![mksafi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mksafi/32/244062_2.png) [@mksafi](https://meta.discourse.org/u/mksafi)
#### Post date: [2022年六月3日 19:24 UTC](https://meta.discourse.org/t/unable-to-lock-post-with-api/228974/3 "2022-06-03T19:24:09Z")

</div>

锁定帖子的 API 在此处文档化：[Discourse API Docs](https://docs.discourse.org/#tag/Posts/operation/lockPost%EF%BC%8C%E4%BD%86%E6%98%AF%E7%9A%84%EF%BC%8C%E6%88%91%E4%B9%9F%E5%B0%9D%E8%AF%95%E8%BF%87%E9%80%86%E5%90%91%E5%B7%A5%E7%A8%8B) Discourse 客户端如何实现它，并且它看起来与我的应用程序中的实现方式非常相似。

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2022年六月4日 06:34 UTC](https://meta.discourse.org/t/unable-to-lock-post-with-api/228974/4 "2022-06-04T06:34:37Z")

</div>

该[代码](https://github.com/discourse/discourse/blob/main/app/controllers/posts_controller.rb#L508)是

` params[:locked] === \"true\" ? locker.lock : locker.unlock`

这似乎是在做一个 **字符串** 比较。

这意味着您需要发送 `\"locked\": \"true\"` 而不是 `\"locked\": true`，并且文档或代码需要更新以反映或更改此行为。

浏览器不发送 JSON 请求，它发送 `Content-Type: application/x-www-form-urlencoded`，它会将参数解释为字符串，这就是它在浏览器中有效的原因。

---

<div class="post-metadata">

### Author: ![mksafi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mksafi/32/244062_2.png) [@mksafi](https://meta.discourse.org/u/mksafi)
#### Post date: [2022年六月4日 16:18 UTC](https://meta.discourse.org/t/unable-to-lock-post-with-api/228974/5 "2022-06-04T16:18:16Z")

</div>

谢谢你的帮助，Richard！

> [@RGJ](#):
>
> `Content-Type: application/x-www-form-urlencoded` 这将把参数解释为字符串

TIL！

我将研究为文档提交一个更新。

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2022年七月4日 16:18 UTC](https://meta.discourse.org/t/unable-to-lock-post-with-api/228974/6 "2022-07-04T16:18:44Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
