首页 > 解决方案 > Postman 测试 - 使用变量解析 JSON 请求时出错

问题描述

我的请求正文如下所示 在此处输入图像描述

“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}} ^

标签: postmanpostman-pre-request-script

解决方案


The error is from

JSON.parse(request.data)

The error says :

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

THis indicate that public_token variable was not resolved , if it was resolved the error would be something like (assuming value of variable is pet)

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

So check whether correct environment or variable is defined

Output:

  1. when environment not selected

  2. when environment was selected

enter image description here


推荐阅读