用python django做微信开发,设置用户分组时,如下代码出现错误:
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of ty
pe str.
url='https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token='+access_token
data={'openid':str(openid),'to_groupid':str(groupid)}
data = json.dumps(data)
request=urllib.request.Request(url)
result=urllib.request.urlopen(request,data).read()
print(result)
解决办法,加入:data=bytes(data,'utf8')
url='https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token='+access_token
data={'openid':str(openid),'to_groupid':str(groupid)}
data = json.dumps(data)
data=bytes(data,'utf8')
request=urllib.request.Request(url)
result=urllib.request.urlopen(request,data).read()
print(result)