邮递员测试 - 使用变量解析 JSON 请求时出错

问题描述

我的请求正文如下所示

enter image description here

“pulic_token”变量已经定义好了。

我的预请求测试如下所示:

pm.test("Public token is correct",() => {
    const req = JSON.parse(request.data);
    pm.expect(req.public_token).to.be.a("string");
});

错误

JSONError: Unexpected token 'p' at 4:21 "public_token": {{public_token}} ^

解决方法

每个“字符串”都必须用双引号 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Cilim</title> </head> <body> <canvas id="cilim" width="550px" height="550px"></canvas> </body> </html>

,

错误来自

Set Null

错误说:

JSON.parse(request.data)

这表明 public_token 变量没有被解析,如果它被解析,错误将类似于(假设变量的值是 pet)

JSONError: Unexpected token 'p' at 4:21 "public_token": {{public_token}} ^

所以检查是否定义了正确的环境或变量

输出:

  1. 未选择环境时

  2. 选择环境时

enter image description here