简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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