requests Use body.encode('utf-8') if you want to send it encoded in UTF-8
基本环境
使用 requests 模块发送 post 请求,请求体包含中文报错
系统环境:centos7.3
python版本:python3.6.8
请求代码:
// 得到中文
param_json = param and json.dumps(param, ensure_ascii=False) with requests.Session() as session:
resp = session.post(url, data=param_json, timeout=HTTP_POST_TIMEOUT, headers=headers, **kwargs)
汉字报错,报错详细内容: 'latin-1' codec can't encode characters in position 545-547: Body ('未识别') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8
解决方法
数据在网络中都是通过字节数据进行传输的, 在发送数据时, requests 模块需要将字符串编码成 bytes 进行传输.
而请求体 body 里面有汉字,requests里边的 URL 编码方式默认是 拉丁 编码,无法对中文内容进行编码
解决方式就是手动使用 utf-8 字符集对 data 进行编码.
// 得到中文
param_json = param and json.dumps(param, ensure_ascii=False) with requests.Session() as session:
resp = session.post(url, data=param_json.encode("utf-8"), timeout=HTTP_POST_TIMEOUT, headers=headers, **kwargs)
参考脚本之家:https://www.jb51.net/article/140386.htm
requests Use body.encode('utf-8') if you want to send it encoded in UTF-8的更多相关文章
- requests模块报错:Use body.encode('utf-8') if you want to send it encoded in UTF-8.
在做 企业向微信用户个人付款 功能时,调用第三方sdk,在 进行 requests 的post请求时, 代码如下 req = requests.post(url, data=data,cert(ap ...
- Python - requests发送请求报错:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 13-14: 小明 is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.
背景 在做接口自动化的时候,Excel作为数据驱动,里面存了中文,通过第三方库读取中文当请求参数传入 requests.post() 里面,就会报错 UnicodeEncodeError: 'lati ...
- python encode和decode函数说明【转载】
python encode和decode函数说明 字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在p ...
- python requests get/post
基本Get请求: #-*- coding:utf-8 -*- import requests url = 'http://www.baidu.com' r = requests.get(url) pr ...
- python之decode、encode及codecs模块
一.先说说编解码问题 编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码. Eg: str ...
- 利用Requests库写爬虫
基本Get请求: #-*- coding:utf-8 -*- import requests url = 'http://www.baidu.com' r = requests.get(url) pr ...
- 【转 记录】python中的encode以及decode
字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...
- python requests的使用说明
#GET参数实例 requests.get('http://www.dict.baidu.com/s', params={'wd': 'python'}) #或 url = 'http://www.b ...
- codecs模块, decode、encode
使用codecs模块,在Python中完成字符编码 字符的编码是按照某种规则在单字节字符和多字节字符之间进行转换的某种方法.从单字节到多字节叫做decoding,从多字节到单字节叫做encodin ...
随机推荐
- Matrix学习
package com.loaderman.customviewdemo; import android.app.Activity; import android.graphics.ColorMatr ...
- @Deprecated注解
它的作用是对不应该再使用的方法添加注解,当编程人员使用这些方法时,将会在编译时显示提示信息,它与javadoc里的@deprecated标记有相同的功能,准确的说,它还不如javadoc @depre ...
- List的remove()方法的三种正确打开方式
转: java编程:List的remove()方法的三种正确打开方式! 2018年08月12日 16:26:13 Aries9986 阅读数 2728更多 分类专栏: leetcode刷题 版权声 ...
- Python网络编程之TCP套接字简单用法示例
Python网络编程之TCP套接字简单用法示例 本文实例讲述了Python网络编程之TCP套接字简单用法.分享给大家供大家参考,具体如下: 上学期学的计算机网络,因为之前还未学习python,而jav ...
- mysql常用命令、非交互式mysql命令看29条
CentOS下mysql数据库常用命令总结1.更改root密码 mysqladmin -uroot password 'yourpassword' 2.远程登陆mysql服务器 mysql -uroo ...
- 【Leetcode_easy】1078. Occurrences After Bigram
problem 1078. Occurrences After Bigram 题意 solution: class Solution { public: vector<string> fi ...
- web系统整体优化
关于web系统整体优化提速总结 关于web系统整体优化提速总结 一.背景 随着公司业务的拓展,随之而来就是各种系统横向和纵向的增加,PV.UV也都随之增加,原有的系统架构和模式慢慢遇上了瓶颈,需要 ...
- python set集合(16)
在python变量中除了以前文章所提到的整形int / 浮点数float / 布尔值bool / 列表list / 字典dict 之外,还有一个类型我们还没有做详细介绍,这个变量类型就是集合set. ...
- 日常工作问题解决:Redhat6.5--解决yum无法正常安装配置问题
1.问题描述 解决RedHat6.5下yum功能不能用问题: 在redhat6.5下使用yum安装时,会提示:This system is not registered to Red Hat Subs ...
- K8S从入门到放弃系列-(12)Kubernetes集群Coredns部署
摘要: 集群其他组件全部完成后我们应当部署集群 DNS 使 service 等能够正常解析,1.11版本coredns已经取代kube-dns成为集群默认dns. 1)下载yaml配置清单 [root ...