Python error: Type error: POST data should be bytes; also user-agent issue

前端 未结 2 660
余生分开走
余生分开走 2020-12-18 23:25

Using the following code I received an error:

TypeError: POST data should be bytes or an iterable of bytes. It cannot be str

Second concern

2条回答
  •  梦毁少年i
    2020-12-18 23:57

    You can try with requests module as an alternative solution

    import json
    import requests
    
    url = 'http://getliberty.org/contact-us/'
    user_agent = 'Mozilla/5.0 (compatible; Chrome/22.0.1229.94; Windows NT)'
    values = {
          'Your Name' : 'Horatio',
          'Your Email' : '6765Minus4181@gmail.com',
          'Subject' : 'Hello',
          'Your Message' : 'Cheers'
           }
    
    headers = {'User-Agent': user_agent, 'Content-Type':'application/json' }
    
    data = json.dumps(values)
    request = requests.post(url, data=data, headers=headers)
    
    response = request.json()
    

提交回复
热议问题