简体   繁体   English

在 python 中使用字典作为参数与请求 API

[英]Using dictionary as parameters with request API in python

I'm working with the request API to run a get request from the gravity form API from wordpress, and having some difficulty in using the search function in the params field. I'm working with the request API to run a get request from the gravity form API from wordpress, and having some difficulty in using the search function in the params field. Below is the working code which I tried at the beginning.下面是我一开始尝试的工作代码。

url = "https://websitename.com/wp-json/gf/v2/forms/100/entries?search={\"start_date\":\"2021-10-01\",\"end_date\":\"2021-10-30\"}"

params={"_field_ids":"id,date_created","paging[page_size]":"100"}

headers = {
  'Authorization': 'Basic Key',
  'Cookie': 'session'
}

response = requests.request("GET", url, headers=headers, data=params)

However, when I changed the param into like this但是,当我将参数更改为这样

params={"search":{"start_date":"2021-07-01","end_date":"2021-07-31"},"_field_ids":"id,date_created","paging[page_size]":"100"}

It does not work anymore.它不起作用了。

I found some resources to put the date range dictionary into a list like this, but it still not working.我找到了一些资源将日期范围字典放入这样的列表中,但它仍然无法正常工作。

params={"search":[{"start_date":"2021-07-01","end_date":"2021-07-31"}],"_field_ids":"id,date_created","paging[page_size]":"100"}

Please help me!!!!请帮我!!!!

The solution is to make the param search become a string by adding ", which is like …"search":"{\"start_date\":\"…解决方法是通过添加“使param search变成一个字符串,就像…"search":"{\"start_date\":\"…

The full code will look like this.完整的代码将如下所示。

url = "https://websitename.com/wp-json/gf/v2/forms/100/entries"

params={"search":"{\"start_date\":\"2021-10-01\",\"end_date\":\"2021-10-30\"}","_field_ids":"id,date_created","paging[page_size]":"100"}

headers = {
  'Authorization': 'Basic Key',
  'Cookie': 'session'
}

response = requests.request("GET", url, headers=headers, params=params)

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

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