Dan c
Dan c

Reputation: 51

List of PostParameters (Python Requests)

Hello So I have a list that I am trying to change the username and go down the list one at a time as it sends a request. For example I have a finallist.txt file that reads:

pop
boat
oreo

these are the list of usernames I want to send as the request. Here is my code:

    list = open("finallist.txt", 'r')
    paramsGet = {"Name": list}
    headers = {<ignore>}
    response = session.get("https://<ignore>.com/api/users/available", params=paramsGet, headers=headers)
    if response.status_code == 200:
        #
    elif response.status_code == 409:
        #

Note: I have all the required headers and syntax correct when the paramsGet - {"Name": pop} but when I try to have it rotate down the list I am lost. I assume I must do for x in range 3 try:? I do not know sorry if I did not make this clear.

Upvotes: 2

Views: 45

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195573

You want to use for-loop, for example:

# load every line from the file to list `lines`
with open("finallist.txt", 'r') as f_in:
    lines = [line for line in map(str.strip, f_in) if line]

# iterate over every line, change headers and do a request
for line in lines:
    print('Name {}'.format(line))
    paramsGet = {"Name": line}    
    headers = {}

    response = session.get("https://<ignore>.com/api/users/available", params=paramsGet, headers=headers)

    if response.status_code == 200:
        # handle status 200
    elif response.status_code == 409:
        # handle status 409

Note: Don't use variable name list - you're "shadowing" Python's builtin.

Upvotes: 1

Related Questions