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 ...
随机推荐
- spring (反射+代理+DI+AOP)
spring https://baijiahao.baidu.com/s?id=1620606848227713760&wfr=spider&for=pc 反射 https://bl ...
- 阿里云RDS与ECS服务器数据库做主从
阿里云RDS与ECS服务器数据库做主从 [精] 里云RDS(数据库)基于飞天大规模分布式计算和存储能力,提供超高性价比的单机版实例,同时利用读写分离横向扩展读能力,满足网站类的业务需求.提供稳定.高性 ...
- window server 2008 iis7+php安装配置
安装环境支持 Microsoft Visual C++ 2012 net framework 4.5 php配置 precision = 20 serialize_precision = 100 ...
- Quartz调度系统入门和调度高可用实现方案
** 版本:2.2.1 ** Hello world: https://www.jianshu.com/p/810400e6a274
- 请求头User-Agent作用?
请求头User-Agent作用 答: User Agent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent也简称UA.它是一个特殊字符串头,是一种向访问网站提供你所 ...
- 123457123456#0#-----com.threeapp.HeiXIanBuNengPeng01----黑线不能碰
-com.threeapp.HeiXIanBuNengPeng01----黑线不能碰
- 123457123457---com.threeObj03.MaJiangertong--- 记忆翻牌益智游戏
com.threeObj03.MaJiangertong--- 记忆翻牌益智游戏
- python面向对象之类属性,实例属性
python中的属性分为类属性和实例属性,之前已经说过一些,这里主要是对类属性与实例属性的增删改查 首先是对类属性的增删改查,下面这个是对类属性的修改,在书写类时,已经对类属性occupation进行 ...
- 【Leetcode_easy】706. Design HashMap
problem 706. Design HashMap solution1: class MyHashMap { public: /** Initialize your data structure ...
- spring mvc框架+ ajax实现 文件上传
1.前端页面,通过form表单提交,必须设置 enctype="multipart/form-data" 代表form表单在发送到服务器时候编码方式是二进制类型,一般用于图片.mp ...