简体   繁体   English

如何在 python requests.get 中将嵌套字典作为参数发送?

[英]How to send nested dictionary as params in python requests.get?

import requests
qp_args = {"query_params": {"list1": [], "list2": [], "list3": [],
                   "data": "something"}}
data = requests.get(url="Service URL", params = qp_args, timeout=2)

# This doesn't work for me, since the client is receiving query_params in chunks, like,

# url/?query_params=list1&query_params=list2&query_params=list3&query_params=data

what is the correct way to send nested dictionary in the query_params in request.get?在 request.get 的 query_params 中发送嵌套字典的正确方法是什么?

You can try this你可以试试这个

import json
import requests

query_params = {
    "list1": [],
    "list2": [],
    "list3": []
}
qp_args = {
    "query_params": json.dumps(query_params),
    "data": "something"
}

data = requests.get(url="Service URL", params = qp_args, timeout=2)

Just try as documentation points out in this link只需按照此链接中的文档指出即可尝试

payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
r = requests.get('https://httpbin.org/get', params=payload)

print(r.url)
# https://httpbin.org/get?key1=value1&key2=value2&key2=value3

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

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