0

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)

3 Answers 3

1

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 =.

Sign up to request clarification or add additional context in comments.

Comments

1
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'}

Comments

1

you can give as follows

url = url + '?'+ your_parm_string
r = requests.get(url, headers=headers)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.