Search code examples
discourse

How can I update topic content using Discourse API?


I have tried to use PUT /t/:id to update topic content with no effect.

https://github.com/discourse/discourse_api/blob/master/lib/discourse_api/api/topics.rb

Seems there is no way to update the content using API. Am I missing anything?


Solution

  • You need to provide the topic slug like this: PUT /t/:slug/:id as documented here: http://docs.discourse.org/#tag/Topics%2Fpaths%2F~1t~1%7Bslug%7D~1%7Bid%7D.json%2Fput

    The /t/:id endpoint only works if you issue a GET request as documented here: http://docs.discourse.org/#tag/Topics%2Fpaths%2F~1t~1%7Bid%7D.json%2Fget which I guess is not what you want.

    The solution

    In Discourse land, a topic it's just a bunch of posts. A topic has no body, the first post of the topic is the body.

    So, what you do is this:

    1. GET /t/:id with your topic id
    2. Parse the post_stream and get the first post, or whichever you need. Get the ID
    3. PUT /posts/:id and use the ID you just got, and provide post[raw] in the body.

    Please, see this discussion: https://meta.discourse.org/t/updating-topic-body-via-the-api/61220/5