Reputation: 37
how do i get/capture the query strings of POST method and turn into dictionary in python? this is the content of request.txt file. FYI the query strings parameter may varies depend on the request file.
POST /login HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/62.0
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost/login
Cookie: __cfduid=dcd3e07532; XSRF-TOKEN=eyJpdiIyYzc0In0%3D; key_session=eyJpdiI6IFmZCJ9
_token=VCXB1YcU3ti&email=testmail%40gmail.com&password=pass12345
this is my code:
from urllib.parse import urlparse, parse_qs
#read last line
with open("request.txt", "r") as file:
first_line = file.readline()
for last_line in file:
pass
Is there any best ways to get the query string using split or other ways? without refering to last line in request.txt file? thanks a lot
Upvotes: 0
Views: 140
Reputation: 11505
from urllib import parse
with open("data.txt") as f:
lines = f.readlines()
goal = lines[lines.index("\n")+1]
print(dict(parse.parse_qsl(parse.urlsplit(goal).path)))
Output:
{'_token': 'VCXB1YcU3ti', 'email': '[email protected]', 'password': 'pass12345'}
Upvotes: 1