postman - 获取body的值到变量
问题描述
我有一个场景,我需要将主体值传递给环境变量并在另一个 API 中使用它们。在邮递员
下面是本体,
{
"firstName" : "Firstname",
"lastName" : "lastname",
"email" : "{{timestamp}}@test.com",
"password" : "{{timestamp}}",
"country" : 8l16
}
下面是 Pre-req 脚本,
postman.setEnvironmentVariable("timestamp", (new
Date).getTime());
// I have copied the Bodyand paste it in a variable called Obj in
Pre-req
// Then i used the below script to get the body
pm.environment.set("rawBody", JSON.stringify(obj));
但是 timestamp 、 email 和 password 的环境值如下。时间戳值是正确的,其他两个是错误的。
timestamp = 1566076106769
email = {{timestamp}}@test.com
password = {{timestamp}}
时间戳值未在电子邮件和密码中替换,我希望将环境变量值设置为,
预期值,
email = 1566076106769@test.com
password = 1566076106769
那么如何将 body 元素值分配给环境/全局变量以在另一个 API 调用中使用?
解决方案
简单的。您已经设置了环境变量,但从未得到它。"{ }" 在Tests和Pre-request Script的代码中不起作用。让它像这样:
const timestamp = pm.environment.get('timestamp');
email = `${timestamp} @test.com`;
password = timestamp;
推荐阅读
- r - 根据循环时间戳 (s) 在 r 中创建一个新变量以识别重复
- python - 无法访问本地网络上 Raspberry pi 上的 Web 服务器
- javascript - 在 'Node' 上执行 'appendChild' 失败:此节点类型不支持此方法
- visual-studio-code - vscode settings.json - 在启动时运行后台终端应用程序
- javascript - Vuejs:如何显示这样的数组值
- javascript - 将范围绑定到 forEach 循环中的项目
- sse - 如何使用 simd 而不是 avx512 将 uint32 转换为 uint8?
- julia - Julia:尝试在 GPU 上从模型动物园运行示例时出现各种 CuArray 错误
- android - 无法解决:com.twitter.sdk.android:tweet-composer@1.0.2
- azure - Terraform Azure 提供程序 - 静态 VM 加密