How to like a topic using the Discourse API


(sleep) #1

Hey all.
How to like a topic about Discourse Api?


(cpradio) #2

Send a request to https://your_forum_url/post_actions sending the post id as id, and the post_action_type_id of 2.

That will in turn like the particular post for the give discourse username and api_key


(sleep) #3

Please tell me how to get the post_id ?


(cpradio) #4

The topic json will have it, or you can view source and see it as a data attribute.


(sleep) #5

Thank you for the help.


(Stefan Milenkovic) #6

hi all,

i tried to like post with {
post_id : id,
post_action_type_id : 2
}

but still getting 400 bad request.
do i need also to send topic_id?

thanks!


(Blake Erickson) #7

nope.

Do you need to provide your api key? I was just able to get it to work with:


(Stefan Milenkovic) #8

i already have cookie and X-CSRF-Token.

the same calls i made for creating topic and replying to topic.
But when i used same logic for liking comment, it is not working.


(Blake Erickson) #9

Make sure you aren’t trying to like your own post :wink:

How do you know what the post id is?

Can you open up your chrome dev tools and like a post and see what the post id is? Then undo your like. Then try again through the api.


(Blake Erickson) #10

its id not post_id


(Stefan Milenkovic) #11

so i looked in dev console, i thought i was missing “flag_topic”:false
but that doesn’t solves the problem

here it is how my data looks like :
{
“post_id”:6519,“
post_action_type_id”:2,
“flag_topic”:false
}

and the same looks in dev tools, but when i try with API
it wont work.


(Blake Erickson) #12

“post_id” needs to be changed to just “id” still.

so it should look like:

id: 6519
post_action_type_id: 2

(Stefan Milenkovic) #13

ah, ok :slight_smile:

not exactly the same, just like you said.
id instead of post_id

thanks!


(Stefan Milenkovic) #14

ok, now data looks like this,
still no luck :slight_smile:

{
“id”:6516,
“post_action_type_id”:2,
“flag_topic”:false
}

even with the id, it won’t work.


(Stefan Milenkovic) #15

it looks like data format is a little bit different with API and from dev console

when i send through the API i use JSON.stringify({
id : id,
post_action_type_id : 2,
flag_topic:false
})

and in the payload looks like this {“id”:6518,“post_action_type_id”:2}:

and when i look payload of call of forum in console it looks like this :
id:6519
post_action_type_id:2
flag_topic:false

what data is expected on server, json?


(Blake Erickson) #16

I don’t know. I would download postman and maybe Charles see if you can get that working that way and it might shed some like on what your doing wrong.

I don’t think the quotes matter.

Make sure your Content-Type is set correctly maybe.


(Stefan Milenkovic) #17

ok, fixed it.

contentType : 'application/json’
and i was setting it only to contentType : ‘json’,

thanks!


(Kenny Meyer) #18

Where do I get the post_action_type_id for the opposite action (dislike) from?


(cpradio) #19

To remove a like you send a DELETE call to the post passing post_action_type_id of 2

As seen in the Network tab of Dev Tools


(Kenny Meyer) #20

Thanks. Where do you find the post_action_type_id?