今天做接口测试,如下图,入参有中文,使用json.dumps()后,参数中的中文乱码了

python json.dumps()中文乱码UnicodeEncodeError: 'latin-1' codec can't encode characters in position 18-24: Body ('x') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

 

json.dumps()中,ensure_ascii参数可以转码,这个默认是True,下图是我使用了该参数之后的错误码,这是为啥?

python json.dumps()中文乱码UnicodeEncodeError: 'latin-1' codec can't encode characters in position 18-24: Body ('x') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

 

 看错误提示,似乎是编码错误,在一个月黑风高的晚上,灵光一动,似乎可以试试将da转码一次

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 18-24: Body ('老中医接口测试') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

result = self.api.session.post(url, data=da.encode("utf-8"))

这样似乎解决了上面的报错问题

 

相关文章:

  • 2021-12-24
  • 2022-12-23
  • 2021-09-07
  • 2021-09-13
  • 2022-12-23
  • 2021-06-01
  • 2022-02-12
猜你喜欢
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
  • 2021-11-04
  • 2022-01-23
相关资源
相似解决方案