今天在搭接口自动化的时候,使用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'])