NGuyen NGuyen - 3 months ago 11
HTTP Question

Python: How to change variable in http post request

data = {'Email':'myemail@gmail.com','Name':'1','Password':'gfgf65jh56456jh67'}
r = requests.post(url, data=json.dumps(data), headers=headers)


Hello,
I am using above code to send http request and it worked well. However, I want to use for loop to change "Name" variable. So, this is my code after changing

for i in range(1,1000):
data = "{'Email':'myemail@gmail.com','Name':'" + str(i) + "','Password':'gfgf65jh56456jh67'}"
r = requests.post(url, data=json.dumps(data), headers=headers)


However, I got an output from server: {'Message': 'An error has occurred.'}. It is not python error. So, how to fix my code ? Thank you :)

Answer

In the line that changes data, data is not a dictionary any more but a string...

Change data simply by:

data['Name'] = str(i)