1

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.'

Sachin K
  • 339
  • 4
  • 14
  • 1
    I've voted to reopen this question: The issue here is a difference between py2 & 3 string & byte handling in `urlencode` and the print statement. In py2 urlencode returns bytes, but the print statement doesn't format them as bytes. In py3 it returns a string, which is why you need to encode it. (Or pass it bytes-only params like `data = {b'username': b'test'}` – rdrey Oct 22 '20 at 13:10

1 Answers1

-1

You are looking for urllib.parse. This is an example of a code that I use.

address = urllib.parse.quote(address, safe='/:=?&', encoding="utf-8", errors="strict")
address = urllib.request.urlopen(address).read().decode('utf8')
Saelyth
  • 1,694
  • 2
  • 25
  • 42
  • I get Traceback (most recent call last): File "", line 1, in File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 804, in quote raise TypeError("quote() doesn't support 'encoding' for bytes") TypeError: quote() doesn't support 'encoding' for bytes – Sachin K Feb 17 '20 at 04:24