0
import requests
import time

req = requests.session()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
a = open("codes.txt", "r").readlines()
param={
      "ID":a
}

source = req.post("http://www.example.com/api/", data=param, headers=headers)
if "valid" in source.text:
    print(source.text)
else:
     print(source.text)

So i have a file called codes.txt and i have some id's in the file.

1234
12345
12346

And after that i send a request to verify if the id is valid or not.

So when im checking and adding multiple ids in that txt file it becomes 1 id 123412345123456

How i can add a \n to every line and make a request for every id.

Zeko
  • 29
  • 6
  • 1
    "So when im checking and adding multiple ids in that txt file it becomes 1 id 123412345123456" Can you please share how you are generating the text file? – Kaushal Kumar Jul 23 '20 at 21:18
  • It is not generated i just add to the txt files the ids. – Zeko Jul 23 '20 at 21:20
  • 1
    Where do you see the id gets concatenated? Where does it become 123412345123456? You question is not clear. – Kaushal Kumar Jul 23 '20 at 21:21
  • 2
    There are a few things that seem off with your code, could you please clarify what each step does? For example `a` is a list, because `readlines()` returns a list, and so `param.ID` is also a list, which I'm guessing is wrong. What exactly should be going on here? :D – dwb Jul 23 '20 at 21:21
  • a = open("codes.txt", "r").readlines() opens the txt file param={ "ID":a // The paramater is ID and 'a' is the txt file. so it's geting the values from the txt file. so in the request will be ID=1234 } I just want to make 1 request/id in the txt file the id's are 1234 \n 12345 ..... but when im trying with multiple ids it is just checking for 1 or just adding them all together – Zeko Jul 23 '20 at 21:30

1 Answers1

1

Use a loop or iterate over the lines in the file. Here is how the modified version will look like:

import requests
import time

req = requests.session()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
file_lines = open("codes.txt", "r").readlines()
for line in file_lines:
    param={
        "ID":line
    }

    source = req.post("http://www.example.com/api/", data=param, headers=headers)
    if "valid" in source.text:
        print(source.text)
    else:
        print(source.text)

Since each line is a entry that needs to be checked, you need to repeat the request with different parameter each time.

Raz Crimson
  • 187
  • 9
  • That not gonna help me at all, because im gonna have a lot of ids and add them manually is not gonna work out. i want to import the lines from the txt file into the id param. – Zeko Jul 23 '20 at 22:50
  • @Zeko U dont need to manually do that. The code does that iteration for you. Try out my code in place of yours with one id per line in codes.txt. – Raz Crimson Jul 23 '20 at 22:55
  • My bad it's working how i wanted, i forgot to add the space after that and i thought something is wrong.thanks for helping appreciate your time <3 – Zeko Jul 24 '20 at 13:54
  • Also now for some reasons if use this only the last item will be checked corectly... and all the items will become false only the last one who doesn't have \n. So the items what has n\ at the end it gets false for some reasons.. – Zeko Jul 24 '20 at 14:17
  • It looks like the response is affected if there is a \n in the request. {'ID': '669615\n'} it return false if i have multiple values in the txt file only when {'ID': '669615'} without the \n is working fine. do you have any ideea if this can be fixed? – Zeko Jul 24 '20 at 14:32
  • Sorry I forgot about that. Use `"ID":line.replace('\n','')`. That will remove any \n in the ID param – Raz Crimson Jul 24 '20 at 15:51
  • https://stackoverflow.com/questions/12330522/how-to-read-a-file-without-newlines This might help you for further refernce – Raz Crimson Jul 24 '20 at 16:05