GET ha sempre una stringa di query e non ha mai dati POST (è per questo che ricevi l’errore 413 payload too large, poiché non si aspetta alcun dato). Quando si recuperano dati da un server senza apportare modifiche, si usa (quasi) sempre GET e non POST. Dovresti quindi usare params= invece di data=.
Credo che la documentazione per questa specifica chiamata API sia mal formulata (“Request Body schema” indica chiaramente l’uso di POST data), quindi sposto questo problema in Contribute > Bug e non devi pagarmi nulla.
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...