【发布时间】:2020-03-03 10:48:27
【问题描述】:
我正在尝试调用 Product Hunt V2 GraphQL API, 当我使用 Python“请求”库发出 POST 请求时,出现以下错误
{'error': 'Bad Request', 'status': 400}
我在 Postman 中使用 GraphQL 正文类型尝试了相同的请求,并成功获得了 200 状态代码,所以我认为问题出在我在请求正文中传递 GraphQL 代码的方式上。这是我使用的代码:
import requests, json
url = "https://api.producthunt.com/v2/api/graphql"
payload = {
"query": "query { posts(first: 1) { edges { node { id, name } } } }"
}
header = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer MYTOKEN",
"Host": "https://api.producthunt.com/v2/api/graphql"
}
response_decoded_json = requests.post(url, headers=header, data=payload)
response_json
查看引用我尝试使用“json.dumps”在 Json 中对有效负载进行编码,但我总是得到相同的 400 状态代码。
我做错了什么?
【问题讨论】:
标签: python-3.x oauth graphql http-post