# Update a Post, via API PUT: request

**URL:** https://meta.discourse.org/t/update-a-post-via-api-put-request/130252
**Category:** Development
**Tags:** rest-api
**Created:** [October 4, 2019, 3:01pm UTC](https://meta.discourse.org/t/update-a-post-via-api-put-request/130252 "2019-10-04T15:01:16Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![alank](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@alank](https://meta.discourse.org/u/alank)
#### Post date: [October 4, 2019, 3:01pm UTC](https://meta.discourse.org/t/update-a-post-via-api-put-request/130252/1 "2019-10-04T15:01:16Z")

</div>

Just to understand, the API Discourse docs mention this kind of… structure…

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

```

Before I attempt this, does this mean, or well.. what does this mean?

does this mean: `{post: {raw: ..., raw_old: ... }}`

Or simply `{raw: "", raw_old: ""...}`

And also what is `raw_old`? Only says that each element is a “string” of what I must PUT: in the JSON body.

I’m simply attempting to add a line at the top of the post (which was initially created by a Bot) where we need to add a link to another site - note also that towards the goal of having that first paragraph displayed under the Category title, or in the `cooked`

 element to be shown (Defined just under the category title).

Hope this makes sense. If not ask me to clarify.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [October 4, 2019, 3:17pm UTC](https://meta.discourse.org/t/update-a-post-via-api-put-request/130252/2 "2019-10-04T15:17:10Z")

</div>

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:

> [@Reverse engineer the Discourse API](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576):
>
> Discourse is backed by a complete JSON api. Anything you can do on the site you can also do using the JSON api. The API is documented at [docs.discourse.org](https://docs.discourse.org). You can also use the [discourse\_api](https://github.com/discourse/discourse_api) Ruby gem as a client library. However, not every endpoint is documented. To determine how to do something with the JSON API here are some steps you can follow. Example: recategorize a topic. Go to a topic and start editing a category: Open Chrome dev tools, switch to the Network tab, select …

This is also how the discourse\_api gem does it:

> <https://github.com/discourse/discourse_api/blob/main/lib/discourse_api/api/posts.rb#L30>

---

<div class="post-metadata">

### Author: ![alank](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@alank](https://meta.discourse.org/u/alank)
#### Post date: [October 8, 2019, 12:58pm UTC](https://meta.discourse.org/t/update-a-post-via-api-put-request/130252/3 "2019-10-08T12:58:59Z")

</div>

> [@blake](#):
>
> documented in the [discourse\_api](https://github.com/discourse/discourse_api) gem

Just a question on this, are you referring to different documentation then what is defined at [Discourse API Docs](https://docs.discourse.org/#tag/Posts/paths/~1posts~1%7Bid%7D.json/put) ? 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?

---

<div class="post-metadata">

### Author: ![alank](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@alank](https://meta.discourse.org/u/alank)
#### Post date: [October 10, 2019, 12:47pm UTC](https://meta.discourse.org/t/update-a-post-via-api-put-request/130252/4 "2019-10-10T12:47:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![md-misko](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/md-misko/32/126315_2.png) [@md-misko](https://meta.discourse.org/u/md-misko)
#### Post date: [December 3, 2019, 7:28pm UTC](https://meta.discourse.org/t/update-a-post-via-api-put-request/130252/5 "2019-12-03T19:28:35Z")

</div>

There is a slight discrepancy in [API Docs](https://docs.discourse.org/#tag/Posts/paths/~1posts~1%7Bid%7D.json/put):

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

`Content type: application/json`

```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`

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

```

The `raw` parameter is sufficient for replacing the entire post.

---

<div class="post-metadata">

### Author: ![alank](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@alank](https://meta.discourse.org/u/alank)
#### Post date: [January 6, 2020, 12:54pm UTC](https://meta.discourse.org/t/update-a-post-via-api-put-request/130252/7 "2020-01-06T12:54:09Z")

</div>

Yes, thanks, I realized this after attempting a few Postman requests and playing around with it… also in my attempts, the “cooked” seemed to do nothing… and I’m still not sure what “raw\_old” is.

---

<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: [October 31, 2023, 8:02pm UTC](https://meta.discourse.org/t/update-a-post-via-api-put-request/130252/8 "2023-10-31T20:02:27Z")

</div>



---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [October 31, 2023, 8:29pm UTC](https://meta.discourse.org/t/update-a-post-via-api-put-request/130252/9 "2023-10-31T20:29:56Z")

</div>

This got bumped today and is assigned to me. Maybe it can be unassigned 🙂

In any case, the documentation is now correct: [https://docs.discourse.org/#tag/Posts/operation/updatePost](https://docs.discourse.org/#tag/Posts/operation/updatePost).
