APIでトピックの本文を更新できません

docsに従いましたが、本文は更新されず、トピックのタイトルのみが更新されました。

私のコード(Javascript):

async function updateDiscourseTopic(topic_id) {
  const Discourse_API_Key = "Discourse_API_Key";
  const url = `https://discourse.mysite.com/t/-/${topic_id}.json`;

  const payload = {
    title: 'session_1',
    raw: "新しい本文(適用されません)"
  }

  const response = await fetch(url, {
    method: 'PUT',
    headers: {
      "Api-Key": Discourse_API_Key,
      "Api-Username": "system",
      "Content-Type": "application/json",
    },
    body: JSON.stringify(payload)
  });

}

コードを見た上での推測ですが、post_id の代わりに topic_id を使用しているからではないでしょうか? Discourse では、最初の投稿がトピックのようなものなので、そうかもしれません。

「いいね!」 3

@Lilly’s right.

トピックは、タイトル、カテゴリ、タグなどの属性によって定義されます。

しかし、コンテンツは投稿に属します。

タイトルとコンテンツを更新したい場合は、2つのリクエストが必要です。

  1. トピックに対するPUT
  2. 最初の投稿に対するPUT。投稿IDはページのHTMLコードで見つけることができます。<article>内のdata-post-id属性を探してください。
    例:
    <article id="post_2" aria-label="post #2 by @Lilly" role="region" data-post-id="1288311" data-
    topic-id="264640" data-user-id="127856" class="boxed onscreen-post">
    
「いいね!」 3

すごい、あなたの言う通りです。うまくいきました :sparkling_heart:

そして、Discourse API を使用してトピックの最初の投稿の ID を取得する方法もわかりました。

async function getFirstPostIdFromOnetopic(topic_id) {
  const Discourse_API_Key = "Discourse_API_Key";
  const url = `https://discourse.mysite.com/t/${topic_id}/posts.json`;

  const response = await fetch(url, {
    method: 'GET',
    headers: {
      "Api-Key": Discourse_API_Key,
      "Api-Username": "system",
      "Content-Type": "application/json",
    }
  });
  const data = await response.json();
  const firstPostId = data.post_stream.posts[0].id;
  
  return firstPostId;
}

ありがとうございます、そして @Lilly さん :innocent:

「いいね!」 3

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