Requests.post() 报错:Body ('') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8


今天在搭接口自动化的时候,使用request.post()发送请求有个报错:

FAILED apply/test_appy_orderIn.py::Test_apply_orderIn::test_apply_orderIn_1 - UnicodeEncodeError: 'latin-1' codec can't encode characters in position 467-475: Body ('工商银行A地支行') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8


问题代码:
r = RequestsHandler().post_Req(url=url, data=yamldict['test_apply_orderIn']['json'], headers=yamldict['test_apply_orderIn']['headers'])

原因:
请求body里面有中文,没有进行utf-8编码

解决方法:
对data进行 encode()编码

代码修改:
r = RequestsHandler().post_Req(url=url, data=yamldict['test_apply_orderIn']['json'].encode(), headers=yamldict['test_apply_orderIn']['headers'])


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM