Reputation: 3561
I'm having trouble constructing a method that will do a HTTP POST request with headers and data (username and password) and will retrieve the resulting cookies.
Here's my latest attempt so far:
def do_login(username, password):
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"}
cj = http.cookiejar.CookieJar()
req = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
data = {"Username": username, "Password": password}
req.open("http://example.com/login.php", data)
But I keep getting exceptions whenever I try to change the method. Also, will the response cookies be stored in the CookieJar cj
, or is that used only for sending request cookies?
Upvotes: 1
Views: 1353
Reputation: 3561
After some research it seems that the data cannot be passed directly as an argument to req.open
, and it needs to be converted to a URL-encoded string. Here's the solution that worked for me:
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"}
cj = http.cookiejar.CookieJar()
req = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
req.addheaders = list(headers.items())
# The data should be URL-encoded and then encoded using UTF-8 for best compatilibity
data = urllib.parse.urlencode({"Username": username, "Password": password}).encode("UTF-8")
res = req.open("http://example.com/login.php", data)
Upvotes: 1