Python 3 urllib produces TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str

前端 未结 3 380
轮回少年
轮回少年 2020-12-01 18:01

I am trying to convert working Python 2.7 code into Python 3 code and I am receiving a type error from the urllib request module.

I used the inbuilt 2to3 Python tool

3条回答
  •  旧时难觅i
    2020-12-01 18:15

    Try this:

    url = 'https://www.customdomain.com'
    d = dict(parameter1="value1", parameter2="value2")
    
    f = urllib.parse.urlencode(d)
    f = f.encode('utf-8')
    
    req = urllib.request.Request(url, f)
    

    Your problem lies in the way you were handling the dictionary.

提交回复
热议问题