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