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 ...
随机推荐
- /bin/sh^M:bad interpreter: No such file or directory问题
脚本命令正确无误,但是执行脚本的时候报错“/bin/sh^M:bad interpreter: No such file or directory” 原因:该脚本文件在windows系统中编辑过,引入 ...
- java模拟post进行文件提交 采用httpClient方法
package com.jd.vd.manage.util.filemultipart; import java.io.BufferedReader;import java.io.File;impor ...
- Android:Recents和AMS中历史任务的区别
1.1 任务和返回栈 - 实际数据模型 这个是指在调度体系里实际保存的TaskRecord实例,而ActivityRecord-TaskRecord-ActivityStack之间的关系建议看官方文 ...
- 解决kali无法连接网络问题
键入 ifconfig -a 查看网卡 ,是否存在 键入 leafpad /etc/network/interfaces 查看其中是否有如下语句,没有添加上即可:auto eth0iface eth0 ...
- spark "main" java.lang.ArrayIndexOutOfBoundsException: 10582
升级 你的 paranamer 到2.8 ,这是由于你的jdk版本1.8导致 <!-- https://mvnrepository.com/artifact/com.thoughtworks.p ...
- QML加载gif
AnimatedImage { anchors.fill: parent source: "qrc:/img/timg.gif" }
- java对list进行排序
主要讲述对list进行排序的几种方式 1.先来个简单的,上代码 import java.util.ArrayList; import java.util.Collections; import jav ...
- jquery图片播放插件Fancybox使用详解
今天给大家介绍的jquery图片播放插件叫Fancybox,相比LightBox来说,Fancybox相对庞大点,配置也更丰富一些,相信你会喜欢的. Fancybox的项目主页地址:http://fa ...
- MG7780打印机喷嘴堵塞
1.深度清洗,打印喷嘴图案,发现有颜色没有打印出来 2.墨盒一加墨水就往外冒,以为是满的,其实是内部已经堵塞而加不进去,因为出墨口并没有墨水向外流(出墨口没有盖起来).解决办法就是用没有针头的针管从加 ...
- python获取昨日日期
获取昨日日期oneday = datetime.timedelta(days=1) 一天 day = datetime.datetime.strptime(self.date,'%Y-%m-%d') ...