json - 如何从请求正文中访问存储为环境变量的 JSON 对象的属性?

标签 json postman

这里我将响应对象存储在环境变量中。

   let res = pm.response.json(); 
   postman.setEnvironmentVariable('currentUser', JSON.stringify(res));

res 对象有一个名为“userId”的属性

在另一个请求正文中,我想将 userId 设置为存储在 currentUser 对象中的值。像这样的东西。
"userId": "{{currentUser.userId}}",

但这没有用。

转换为 JSON 也不起作用。
"userId": "{({JSON.parse(currentUser)).userId}}",

是否可以在 Postman 中执行此操作?

编辑

对象中有许多属性用于不同的其他请求。我在想,如果我可以保存对象并在需要时传递它们,而不是为每个对象创建环境变量。这就是其背后的原因。

最佳答案

这样的事情会为你做同样的事情吗?

let userId = pm.response.json().userId
pm.environment.set('currentUser', userId)

然后像这样使用它:
"userId": "{(userId}}"

不确定在数据中存储单个值的整个响应背后的推理是什么。

编辑

您可以将此添加到 Pre-request Script第二个请求:
pm.environment.set("userId", JSON.parse(pm.environment.get('currentUser')).userId)

然后以与我上面提到的相同的方式在 POST 中引用它请求体。

关于json - 如何从请求正文中访问存储为环境变量的 JSON 对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54457903/

相关文章:

java - Spring Boot从请求中获取图像数据

ios - 重新单击按钮后程序不会更新

json - Golang 不能使用 as type struct array 或 slice literal

python - 从 json 文件加载项目描述

jenkins - BitBucket Webhook 在发布到 Jenkins 时出现 Crumb 问题

javascript - 预期 postman 断言错误 : 'value' to equal value

php - 如何使字符串数组在php中将其发送为JSON格式?

javascript - Json 到嵌套列表 : First node not captured

django - 当我通过基本身份验证在 postman 中使用 PUT 方法时出现错误,我正在研究 django rest-framework

javascript - 从 JSON 响应中获取键名