python2.7 requests.post() 消息体中文乱码的一种解决方法

  1. data = json.dumps(params, ensure_ascii=False)
  2. requests.post(url, data.encode(‘gbk’), headers)
def post(params, route):
    base_url = "http://xxxxxx"
    headers = {"Content-Type": "application/json;charset=utf-8"}
    
    url = base_url + route
    print("Url: {}".format(url))
    
    data = json.dumps(params, ensure_ascii=False)
    print("Request body:\n{}".format(data.encode('gbk')))
    
    response = requests.post(url, data.encode('gbk'), headers)
    print("Response body:\n{}\n".format(response.text))
    ```

你可能感兴趣的:(python)