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

前端 未结 3 365
轮回少年
轮回少年 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条回答
  • 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.

    0 讨论(0)
  • 2020-12-01 18:18

    I used python requests module with ZOHO CRM API V2. It worked without any issues. Here is a sample working code with GET request:

    import json
    import requests
    
    # API methods - https://www.zoho.com/crm/developer/docs/api/api-methods.html
    # getrecords API Call
    module_name = 'Deals'
    authtoken = '*****'
    api_url = "https://crm.zoho.com/crm/private/json/"+module_name+"/getRecords?authtoken="+authtoken+"&scope=crmapi&fromIndex=1&toIndex=2"
    
    # GET Request
    request_response = requests.get(
        url=api_url
        )
    print(json.dumps(json.loads(request_response.text), sort_keys=True, indent=4, separators=(",", ": ")))
    json_response = json.loads(request_response.text)
    
    0 讨论(0)
  • 2020-12-01 18:28

    From the docs Note that params output from urlencode is encoded to bytes before it is sent to urlopen as data:

    data = urllib.parse.urlencode(d).encode("utf-8")
    req = urllib.request.Request(url)
    with urllib.request.urlopen(req,data=data) as f:
        resp = f.read()
        print(resp)
    
    0 讨论(0)
提交回复
热议问题