How to Send POST Request Using urllib Only in Python3

Jumped into Python, having the urllib, urllib2 and urllib3 for Python3; Unfortunate for me as I’m using Python3 there is no urllib2 for it and POST requests in most places I’ve seen it used are used with urllib2. I had no choice than to deal with what I have, digging into Python documentary site I figured out how to send POST requests with urllib only. Below is a sample code

import urllib.request, urllib.parse
data = {
'name' : 'james',
'age' : 'blaba'
data = bytes( urllib.parse.urlencode( data ).encode() )
handler = urllib.request.urlopen( '', data );
print( 'utf-8' ) );


As said in Python doc site,

data must be a bytes object specifying additional data to be sent to the server, or None

Turning the dictionary data into URL data using urlencode, I then encoded the string and turned it into bytes then sent! 🙂 returns bytes data also so I decoded into utf-8 string and do what I want 🙂

NB: Without the data argument, request is sent with GET

Hope it helps…