简体   繁体   English

在 python3 中使用 GraphQL 有效负载发布请求

[英]Post request with GraphQL Payload in python3

Hello I'm trying to make a Post request to ProductHunt API.您好,我正在尝试向 ProductHunt API 发出 Post 请求。 In the example they provide the following payload:在示例中,它们提供以下有效负载:

{
"query": "query { posts(first: 1) { edges { node { id, name } } } }"
}

This is my Python implementation:这是我的 Python 实现:

queryz =  { 'query' : '{ posts(first: 1) { edges { node { id, name } } 
} }' }

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

I tried encoding the payload in Json but I always get the same response我尝试在 Json 中对有效负载进行编码,但我总是得到相同的响应

When I do the request I always get a 400 Bad Request response, I tried the same request in Postman using GraphQL query and I get the correct response.当我发出请求时,我总是收到 400 Bad Request 响应,我在 Postman 中使用 GraphQL 查询尝试了相同的请求,我得到了正确的响应。

What am I doing wrong?我究竟做错了什么?

I solved it.我解决了。 The issue was in the way I used the dumps function.问题在于我使用转储 function 的方式。

Using the following code, it works使用以下代码,它可以工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM