Reputation: 147
What I would like to achieve is given an input ( http parameters ) to the tool, send it back to another server but for each http parameter an extra string to the current values
based on @liakoyras help:
......
attributes = ["AAA", "BBBB", "CCCC" ]
inp = input("HTTP parameters:") #aa=bb&cc=dd&ee=ff
pars = inp.split("&")
for par in pars:
for attribute in attributes:
query = f"{par}({attribute}"
data = {inp.replace(par, query):"extrastring2)"}
print(data)
r = requests.post(url, data=data)
.....
this is the ( messy ) current results I get, expect the last 3:
{'aa=bb(AAA&cc=dd&ee=ff': 'extrastring2)'}
#post request: aa=bb(AAA&cc=dd&ee=ff=extrastring2)
{'aa=bb(BBBB&cc=dd&ee=ff': 'extrastring2)'}
#post request: aa=bb(BBBB&cc=dd&ee=ff=extrastring2)
{'aa=bb(CCCC&cc=dd&ee=ff': 'extrastring2)'}
#post request: aa=bb(CCCC&cc=dd&ee=ff=extrastring2)
{'aa=bb&cc=dd(AAA&ee=ff': 'extrastring2)'}
#post request: aa=bb&cc=dd(AAA&ee=ff=extrastring2)
{'aa=bb&cc=dd(BBBB&ee=ff': 'extrastring2)'}
#post request: aa=bb&cc=dd(BBBB&ee=ff=extrastring2)
{'aa=bb&cc=dd(CCCC&ee=ff': 'extrastring2)'}
#post request: aa=bb&cc=dd(CCCC&ee=ff=extrastring2)
{'aa=bb&cc=dd&ee=ff(AAA': 'extrastring2)'}
#post request: aa=bb&cc=dd&ee=ff(AAA=extrastring2)
{'aa=bb&cc=dd&ee=ff(BBBB': 'extrastring2)'}
#post request: aa=bb&cc=dd&ee=ff(BBBB=extrastring2)
{'aa=bb&cc=dd&ee=ff(CCCC': 'extrastring2)'}
#post request: aa=bb&cc=dd&ee=ff(CCCC=extrastring2)
the last 3 are the perfect results because are valid parameters for the post requests I need to make
So what I wish to obtain is:
aa=bb(AAA=extrastring2)&cc=dd&ee=ff
[..]
aa=bb&cc=dd(AAA=extrastring2)&ee=ff
[..]
aa=bb&cc=dd&ee=ff(AAA=extrastring2)
Upvotes: 0
Views: 74
Reputation: 4137
Is this what you are looking for?:
attributes = ["AAA", "BBBB", "CCCC"]
# inp = input("HTTP parameters:") #aa=bb&cc=dd&ee=ff
inp = r"aa=bb&cc=dd&ee=ff"
pars = inp.split("&")
for par in pars:
for attribute in attributes:
query = f"{par}({attribute}: extrastring2)"
data = inp.replace(par, query)
print(data)
# r = requests.post(url, data=data)
The output from this was:
aa=bb(AAA: extrastring2)&cc=dd&ee=ff
aa=bb(BBBB: extrastring2)&cc=dd&ee=ff
aa=bb(CCCC: extrastring2)&cc=dd&ee=ff
aa=bb&cc=dd(AAA: extrastring2)&ee=ff
aa=bb&cc=dd(BBBB: extrastring2)&ee=ff
aa=bb&cc=dd(CCCC: extrastring2)&ee=ff
aa=bb&cc=dd&ee=ff(AAA: extrastring2)
aa=bb&cc=dd&ee=ff(BBBB: extrastring2)
aa=bb&cc=dd&ee=ff(CCCC: extrastring2)
Upvotes: 1
Reputation: 1185
Your code does not work because it removes the original parameter from the input and then appends the modified one in the end.
You simply need to change calling of replace
to inp.replace(par, query)
in order to put it in the correct position.
input_params = "aa=bb&cc=dd&ee=ff"
pars = input_params.split('&')
for par in pars:
query = par + "(extrastring1" + "=extrastring2)"
print(input_params.replace(par, query))
This code outputs the expected
aa=bb(extrastring1=extrastring2)&cc=dd&ee=ff
aa=bb&cc=dd(extrastring1=extrastring2)&ee=ff
aa=bb&cc=dd&ee=ff(extrastring1=extrastring2)
Upvotes: 1