如何在Posman中将文本与变量值分离



我试图在Postman中将文本与变量值分离。

值我的变量:

{
"id": ObjectID("x1223123x"),
"name": "John",
"tokens": [{
"type": "activation",
"token": "xX-tokenValue"
}]}

我用捕捉到了它

const $ = cheerio.load(pm.response.text());pm.globals.set("textMongo", $( "textarea[name*='document']" ).text());

我把它存储在文本类型中,因为我把它和html页面分开了。

当我试图将其解析为json时,我遇到了一个问题:

console.log(JSON.parse($( "textarea[name*='document']" ).text()));
JSONError: No value found for key _id at 2:12 "id": ObjectID("x1223123x"),

不管怎样。。。我能做些什么来存储在新变量"的值中;令牌":"xX标记值"?

var res = pm.response.json();
if(res.tokens!=null && res.tokens.lenght>0)
{
pm.globals.set("textMongo",res.tokens[0].token);
}

在您的情况下,响应是JSON格式的,因此您不需要使用String函数来获取令牌。

最新更新