How can I update topic content using API?

api

(Li Xinyang) #1

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

Seems there is no way to update the content using API. :frowning:


(Blake Erickson) #2

Look at the edit_posts method.


(Li Xinyang) #3

@blake Oops. I miss typed Topic to Post. I actually want to update content of a topic.


(cpradio) #4

Such as the Title? Or tags? Or Category?


(Li Xinyang) #5

@cpradio content the content of topic.


(cpradio) #6

That would be updating a post, not a topic. So you’d need to target the post and treat it like an edit.


(Li Xinyang) #7

Thanks for your quick response! I will try this right now!


(cpradio) #8

Just some tips, you can call the topic json, such as, /t/:id.json and it should have a posts array, you want to grab the id for post_number 1 and use that for the edit API call.


(Li Xinyang) #9

Thanks! This is really helpful. I was trying this method or the passed 1 hour with no success.

@cpradio You can get the topic info here http://frontseat.io/t/17.json

{
  "post_stream": {
    "posts": [
      {
        "id": 20,
        "name": "李忻阳",
        "username": "li-xinyang",
        "avatar_template": "/user_avatar/frontseat.io/li-xinyang/{size}/3_1.png",
        "created_at": "2016-04-20T04:20:22.728Z",
        "cooked": "<p><strong>Title</strong>  # Title Level One  <code>javascript var code = 1;</code></p>",
        "post_number": 1,
        "post_type": 1,
        "updated_at": "2016-04-20T04:20:22.728Z",
        "reply_count": 0,
        "reply_to_post_number": null,
        "quote_count": 0,
        "avg_time": null,
        "incoming_link_count": 0,
        "reads": 2,
        "score": 45.4,
        "yours": false,
        "topic_id": 17,
        "topic_slug": "api",
        "display_username": "李忻阳",
        "primary_group_name": null,
        "version": 1,
        "can_edit": false,
        "can_delete": false,
        "can_recover": false,
        "can_wiki": false,
        "read": true,
        "user_title": null,
        "actions_summary": [
          {
            "id": 2,
            "count": 1
          }
        ],
        "moderator": true,
        "admin": true,
        "staff": true,
        "user_id": 1,
        "hidden": false,
        "hidden_reason_id": null,
        "trust_level": 4,
        "deleted_at": null,
        "user_deleted": false,
        "edit_reason": null,
        "can_view_edit_history": true,
        "wiki": false
      },
      {
        "id": 21,
        "name": "李忻阳",
        "username": "li-xinyang",
        "avatar_template": "/user_avatar/frontseat.io/li-xinyang/{size}/3_1.png",
        "created_at": "2016-04-20T04:24:35.276Z",
        "cooked": "<p>测试回复测试回复测试回复测试回复测试回复</p>",
        "post_number": 2,
        "post_type": 1,
        "updated_at": "2016-04-20T04:24:35.276Z",
        "reply_count": 0,
        "reply_to_post_number": null,
        "quote_count": 0,
        "avg_time": null,
        "incoming_link_count": 0,
        "reads": 2,
        "score": 0.4,
        "yours": false,
        "topic_id": 17,
        "topic_slug": "api",
        "display_username": "李忻阳",
        "primary_group_name": null,
        "version": 1,
        "can_edit": false,
        "can_delete": false,
        "can_recover": false,
        "can_wiki": false,
        "read": true,
        "user_title": null,
        "actions_summary": [],
        "moderator": true,
        "admin": true,
        "staff": true,
        "user_id": 1,
        "hidden": false,
        "hidden_reason_id": null,
        "trust_level": 4,
        "deleted_at": null,
        "user_deleted": false,
        "edit_reason": null,
        "can_view_edit_history": true,
        "wiki": false
      },
      {
        "id": 22,
        "name": "李忻阳",
        "username": "li-xinyang",
        "avatar_template": "/user_avatar/frontseat.io/li-xinyang/{size}/3_1.png",
        "created_at": "2016-04-20T04:53:14.587Z",
        "cooked": "<p>Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content</p>",
        "post_number": 3,
        "post_type": 1,
        "updated_at": "2016-04-20T04:53:14.587Z",
        "reply_count": 0,
        "reply_to_post_number": null,
        "quote_count": 0,
        "avg_time": null,
        "incoming_link_count": 0,
        "reads": 2,
        "score": 0.4,
        "yours": false,
        "topic_id": 17,
        "topic_slug": "api",
        "display_username": "李忻阳",
        "primary_group_name": null,
        "version": 1,
        "can_edit": false,
        "can_delete": false,
        "can_recover": false,
        "can_wiki": false,
        "read": true,
        "user_title": null,
        "actions_summary": [],
        "moderator": true,
        "admin": true,
        "staff": true,
        "user_id": 1,
        "hidden": false,
        "hidden_reason_id": null,
        "trust_level": 4,
        "deleted_at": null,
        "user_deleted": false,
        "edit_reason": null,
        "can_view_edit_history": true,
        "wiki": false
      },
      {
        "id": 23,
        "name": "李忻阳",
        "username": "li-xinyang",
        "avatar_template": "/user_avatar/frontseat.io/li-xinyang/{size}/3_1.png",
        "created_at": "2016-04-20T05:19:28.083Z",
        "cooked": "<p>Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content Test Content</p>",
        "post_number": 4,
        "post_type": 1,
        "updated_at": "2016-04-20T05:19:28.083Z",
        "reply_count": 0,
        "reply_to_post_number": null,
        "quote_count": 0,
        "avg_time": null,
        "incoming_link_count": 0,
        "reads": 2,
        "score": 0.2,
        "yours": false,
        "topic_id": 17,
        "topic_slug": "api",
        "display_username": "李忻阳",
        "primary_group_name": null,
        "version": 1,
        "can_edit": false,
        "can_delete": false,
        "can_recover": false,
        "can_wiki": false,
        "read": true,
        "user_title": null,
        "actions_summary": [],
        "moderator": true,
        "admin": true,
        "staff": true,
        "user_id": 1,
        "hidden": false,
        "hidden_reason_id": null,
        "trust_level": 4,
        "deleted_at": null,
        "user_deleted": false,
        "edit_reason": null,
        "can_view_edit_history": true,
        "wiki": false
      },
      {
        "id": 24,
        "name": "Zhang Liang",
        "username": "ZhangLiang",

Here is what I am trying to do (See screenshot below). Keep getting 400. However, it is work fine if I use the same API to create and delete the post.


(cpradio) #10

First off, unless that is a dev machine, strongly suggest you deactivate that API KEY immediately.

Secondly, you are sending it to the correct post, but your form-data is incorrect.

You can remove topic_id, that won’t be used
You need to use post[raw] instead of raw
You can provide a post edit reason using post[edit_reason]

I haven’t tried excluding post[cooked] but that may also be necessary, I’d personally try excluding it first. Maybe @eviltrout or @zogstrip can elaborate on whether that must be sent too.


(Li Xinyang) #11

Thanks for the response and kind reminder. I just reset the API key and your suggestion works perfect fine! Thank you! :blush: