【问题标题】:Post request in python3 with GraphQL Body使用 GraphQL Body 在 python3 中发布请求
【发布时间】: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


    【解决方案1】:

    我解决了,显然我错误地使用了 json.dumps。

    r = requests.post(url, headers=headerz, data=json.dumps(queryz))
    

    【讨论】:

      猜你喜欢
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 2019-03-19
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 2021-07-08
      相关资源
      最近更新 更多