Reputation: 1719
Given is a string:
'search=hello+world&status=something&cache=false'
How do I pass parameters and values from that string to a payload dictionary given that I'm not sure what parameters I will always get from the string in order to use it in requests.get()?
requests.get(url, params=payload, headers=headers)
Upvotes: 0
Views: 92
Reputation: 19875
Use a dict
comprehension:
def split_params(param_string):
return {param: value for param, value in (pair.split('=') for pair in param_string.split('&'))}
split_params('search=hello+world&status=something&cache=false')
Output:
{'search': 'hello+world', 'status': 'something', 'cache': 'false'}
This is based off the observation that each param-value pair is separated externally by an &
and internally by an =
.
Upvotes: 1
Reputation: 1135
you can give as follows
url = url + '?'+ your_parm_string r = requests.get(url, headers=headers)
Upvotes: 1
Reputation: 20490
string = 'search=hello+world&status=something&cache=false'
params = string.split('&')
payload = {}
for params in param:
p = param.split('=')
payload[p[0]] = p[1]
print(payload)
{'search': 'hello+world', 'status': 'something', 'cache': 'false'}
Upvotes: 1