GET 始终带有查询字符串,从不包含 POST 数据(这就是您收到 413 Payload Too Large 错误的原因,它不期望任何数据)。在查询服务器数据而不进行更改时,您几乎总是使用 GET 而不是 POST。因此,您应该使用 params= 而不是 data=。
我认为此特定 API 调用的文档措辞不当(“请求正文架构”实际上表明是 POST data),因此我将其移至 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...\n```