Python 2.7 requests库发送POST请求,请求体是JSON格式,JSON字符串中包含中文,可以如此处理:
# coding: utf-8
或者
# -*- coding: utf-8 -*-
注意:中文字符串前不要加u,如u’测试’,字符串前加u代表使用unicode编码该字符串(utf8编码汉字占3个字节。在UTF-8编码中,一个中文等于三个字节,一个中文标点占三个字节;而在Unicode编码中,一个中文(含繁体)等于两个字节。)
url = "http://xxxxxxxxxxx"
headers = {"Content-Type": "application/json;charset=utf-8"}
data = json.dumps(params, ensure_ascii=False)
response = requests.post(url, data, headers)