如何使用Python的requests库从API中获取JSON查询响应?

huangapple go评论69阅读模式
英文:

How fetch a json query response from an api in python using requests library?

问题

我想在Python的requests.get方法中将JSON查询作为参数传递,并从URL获取基于查询的响应。

query_params = {
  "query": {
    "exists": {
      "field": "user_id"
    }
  },
  "size": 10000
}

headers = {"X-API-Key": api_key, "Content-Type": "application/json"}

response = requests.post(url, params=json.dumps(query_params), headers=headers)

print(response.json())

这里的输出不基于查询参数,而是基于正常响应,即使我们不传递查询也会得到相同的响应。

我尝试了get方法:

response = requests.get(url, params=query_params, headers=headers)

两者都给我一个不基于传递查询的响应。我还尝试了在Postman中使用相同的查询,它给了我正确的响应。我确保了Postman和我的脚本中的标头是相同的。标头格式是X-API-Key:

如何正确地传递一个基于查询的请求以获取基于查询的响应?

英文:

I want to pass a json query as an argument in the requests.get method in python and get the query based response from the url.

query_params = {
  "query": {
    "exists": {
      "field": "user_id"
    }
  },
  "size":10000
}

headers = {"X-API-Key": api_key, "Content-Type": "application/json"}

response = requests.post(url, params=json.dumps(query_params), headers=headers)

print(response.json())

Here the output is not based on the query parameters, it is the normal response we get even if we don't pass the query.

I tried the get method

response = requests.get(url, params=query_params, headers=headers)

Both gave me a response that is not based on the passed query. I have also tried the same query in postman that gives me the correct response. I ensured that the headers are the same in both postman and in my script. The header format is X-API-Key:<token>.

What is the correct way to pass a query request in the requests library to get a query based response?

答案1

得分: 1

要在Python的requests.get()方法中将JSON查询作为参数传递,您需要使用json参数而不是params参数。params参数用于在URL中传递查询参数,而json参数用于在请求体中发送JSON数据。

import requests
import json

query_params = {
  "query": {
    "exists": {
      "field": "user_id"
    }
  },
  "size": 10000
}

headers = {"X-API-Key": api_key, "Content-Type": "application/json"}

response = requests.get(url, json=query_params, headers=headers)

print(response.json())
英文:

To pass a JSON query as an argument in the requests.get() method in Python, you need to use the json parameter instead of the params parameter. The params parameter is used to pass query parameters in the URL, while the json parameter is used to send JSON data in the request body.

import requests
import json

query_params = {
  &quot;query&quot;: {
    &quot;exists&quot;: {
      &quot;field&quot;: &quot;user_id&quot;
    }
  },
  &quot;size&quot;: 10000
}

headers = {&quot;X-API-Key&quot;: api_key, &quot;Content-Type&quot;: &quot;application/json&quot;}

response = requests.get(url, json=query_params, headers=headers)

print(response.json())

答案2

得分: 0

使用json而不是params

response = requests.post(url, json=query_params, headers=headers)

这个方法有效!

英文:

Use json instead of params

response = requests.post(url, json=query_params, headers=headers)

This worked!

答案3

得分: 0

你可以从你的Postman请求生成Python代码。
我认为这将解决你的问题,也会让你了解你之前做错了什么。

在这里查看:Postman:生成客户端代码

英文:

You can generate python code from your postman request.
I think it will solve your problem, also it will give you understanding of what you was doing wrong.

Here check this out Postman: Generating client code

huangapple
  • 本文由 发表于 2023年6月8日 16:02:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定