我在进行pytest框架写自动化测试的时候出现了下面问题

pytest框架报错信息总是显示如下信息时👇

2025-06-24 10:19:53,444 - api_test - DEBUG - 响应内容: {'msg': "JSON parse error: Unrecognized token 'id': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'id': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 4]", 'code': 500}

查了之后发现定位问题时

通常问题出现在你的接口语句上
 

response = api_client.send_request(method, api_url, params=params, json=data)

你看出这里面的错误了吗??

修改问题?

  1. 问题根源

    • 使用 data=data 发送请求时,默认是 application/x-www-form-urlencoded 格式

    • 服务端期望的是 application/json 格式

    • 您发送的数据实际是:id=123&type=4(表单格式)

    • 服务端收到的是:id=123&type=4(非 JSON 格式)

  2. 解决方案

    • 使用 json=data 参数:

      • 自动设置 Content-Type: application/json

      • 自动将字典转换为 JSON 格式

    • 发送的数据变为:{"idss": xxx, "tape": 4}(JSON 格式)

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐