I'm making a POST request to a remote server using Python 2.7 and Python 3 and getting different responses in each scenario. Could the issue be how the data in each request is being encoded? What's the Python 3 equivalent of what I'm doing in 2.7?
// Python 2.7
data = {'username':'test'}
data = urllib.urlencode(data)
print(data)
req = urllib2.Request(base_link + 'inf.cgi', data, headers)
response = urllib2.urlopen(req)
mypage = response.read()
user=test
// Python 3
data = {'username':'test'}
data = urllib.parse.urlencode(data).encode("utf-8")
print(data)
base_link = 'http://tax1.co.monmouth.nj.us/cgi-bin/'
# Make HTTP request
request = urllib.request.Request(url, data, headers)
mypage = urllib.request.urlopen(request).read()
b'user=test'
Trying this without encoding the data:
data = urllib.parse.urlencode(data)
gives a similar output to that in 2.7:
user=test
but results in an error when making the POST request: 'TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.'