Discourse API docs mention a "request body" for a GET request

I’m trying to create a script to get specific posts from topic. See this page Discourse API Docs

But I keep getting response status code = 413.

Somebody help me, please! Will send $20 to your PayPal account if you can solve it.

def get_post_from_topic(topic_id, post_id):
    endpoint = "https://www.example.com/t/" + topic_id + "/posts.json"

    headers = {
        "Content-Type": "application/json",
        "Api-Key": api_key,
        "Api-Username": api_username,
    }

    data = {"post_ids[]": post_id}  # I tried 0, didn't work

    response = requests.get(endpoint, headers=headers, data=data)
    if response.status_code == 200:
        return response

FYI my Api-Key and Api-Username are correct. My other discourse APIs work except for this one.

Hi, you can check out Discourse API gen

You will easily find the api you need in this gem so you don’t have to implement it separately.

If you still need paid support do let us know will move the topic to marketplace so interested person can contact you

1 Like

Hey @Ahmed_Gagan

Thanks for the reply.

I think I still need paid support.

I moved the post to marketplace

You are sending a body, you should be using a post request.

1 Like

Just get the topic and sort out the posts you want by ignoring the others.

Also, if you have the post id then you can get /p/123

I tried the retrieve a single post api. It doesn’t return “link_counts” and doesn’t have “post_stream”.

I think get specific posts from topic API has “post_stream”.

And the topic I’m trying to process has over 1500 posts…

1 Like

Maybe you need to encode your data explicitly as json?

data = '{"post_ids[]":' + post_id +'}' 

It doesn’t work.

I just tried json.dumps as well. Doesn’t work…

I wish we have some code examples…

GET always has a query string and never has post data (that is why you are getting 413 payload too large, it does not expect any). When querying data from a server without making a change you’ll (almost) always use GET and not POST. So you should use params= instead of data=.

I think the docs for this specific API call are badly phrased (“Request Body schema” really indicates POST data) so I am moving this to bug and you don’t need to pay me anything.

import requests
import json

def get_post_from_topic(topic_id, post_id):
    endpoint = f"https://forum.example.com/t/{topic_id}/posts.json"
    api_username = 'system'
    api_key = 'REDACTED'
    headers = {
        "Content-Type": "application/json",
        "Api-Key": api_key,
        "Api-Username": api_username,
    }
    params = {
      "post_ids[]": post_id
    }
    response = requests.get(endpoint, headers=headers, params = params) 
    if response.status_code == 200:
        return response

response = get_post_from_topic(6,8)
print(response.json())

{"post_stream": {"posts": [{"id": 8, "name": "system", "username": "system", "avatar_template": "/images/discourse-logo-sketch-small.png", "created_at": "2022-06-26T04:44:23.637Z", "cooked": "<p><a name=\"collect\"></a></p>\n<h2>\n<a name=\"what-information-do-we-collectcollect-1\" class=\"anchor\" href=\"#what-information-do-we-collectcollect-1\"></a><a href=\"#collect\">What information do we collect...
5 Likes

It works!

Thank a lot @RGJ

2 Likes

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