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
Hey @Ahmed_Gagan
Thanks for the reply.
I think I still need paid support.
I moved the post to marketplace
Hector
(Héctor Fernández)
6월 26, 2022, 7:20오후
6
You are sending a body, you should be using a post request.
pfaffman
(Jay Pfaffman)
6월 26, 2022, 7:39오후
7
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…
pfaffman
(Jay Pfaffman)
6월 26, 2022, 7:54오후
10
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…
RGJ
(Richard - Communiteq)
6월 26, 2022, 10:19오후
13
GET 요청에는 항상 쿼리 문자열이 포함되어 있으며, POST 데이터는 절대 포함되지 않습니다(이것이 413 payload too large 오류가 발생하는 이유이며, 서버는 POST 데이터를 기대하지 않습니다). 서버에서 데이터를 변경하지 않고 조회할 때는 거의 항상 POST가 아닌 GET을 사용합니다. 따라서 data= 대신 params=를 사용해야 합니다.
해당 API 호출에 대한 문서가 다소 불명확하게 작성되어 있다고 생각합니다("Request Body schema "는 실제로 POST data를 암시합니다) 그래서 이 이슈를 #contribute:bug로 옮기겠습니다. 그리고 아무것도 지불하실 필요가 없습니다.
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...