python - 使用python发布json数据时出现问题
问题描述
我正在尝试从 LeetCode-CN 获取我提交的所有代码,并且我需要获取问题的翻译。下面的 curl 命令可以给我想要的结果:
curl 'https://leetcode-cn.com/graphql' -H 'authority: leetcode-cn.com' -H 'origin: https://leetcode-cn.com' -H 'content-type: application/json' -H 'referer: https://leetcode-cn.com/problemset/all/' --data '{"operationName":"getQuestionTranslation","variables":{},"query":"query getQuestionTranslation($lang: String) {\n translations: allAppliedQuestionTranslations(lang: $lang) {\n title\n questionId\n __typename\n }\n}\n"}'
但是下面的python代码不能工作:
def loadChnProblemList(client):
query = {
"operationName": "getQuestionTranslation",
"variables": {},
"query": "query getQuestionTranslation($lang: String) {\n translations: allAppliedQuestionTranslations(lang: $lang) {\n title\n questionId\n __typename\n }\n}\n"}
headers = {
"content-type": "application-json",
"origin": "https://leetcode-cn.com",
"referer": "https://leetcode-cn.com/problemset/all/"
}
response = requests.post("https://leetcode-cn.com/graphql", headers=headers, data=json.dumps(query))
data = json.loads(response.text)
print(data)
服务器将响应:
{"errors":[{"message":"Must provide query string."}]}'
为什么 curl 命令有效但 python 代码无效?
解决方案
Shijith 是正确的 - 将内容类型更改为 application/json 是解决方法。有关更多详细信息,请参阅请求文档。
推荐阅读
- google-apis-explorer - android-publisher 验证测试
- python - 如何访问 json https://i.instagram.com/api/v1/tags/hashtag/sections/?
- python - 保留对的标题
- python - 如何在从循环中附加数据时解析无效的json
- python - matplotlib 标题()不显示
- kernel - 为什么设计 RCU 是为了保护指针分配?
- r - 我有一个面板数据集,每年有多个观察值,我如何计算每年 obs 的总和而不对“年份”变量求和?
- java - Jackson ObjectMapper 两次用不同的值写入对象
- laravel - 后续npm操作遇到权限问题
- php - PHP 简单的网络套接字