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