GET 请求始终带有查询字符串,且从不包含 POST 数据(这就是你遇到 413 Payload Too Large 错误的原因,因为它不期望接收任何数据)。当从服务器查询数据且不进行修改时,你(几乎)总是应该使用 GET 而不是 POST。因此,你应该使用 params= 而不是 data=。
我认为该特定 API 调用的文档表述不佳(“请求体 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\">我们收集什么信息...