gpt4 book ai didi

dynamic - 如何在 postman 中传递动态json请求正文

转载 作者:行者123 更新时间:2023-12-04 13:22:27 26 4
gpt4 key购买 nike

我有一个 POST 请求,我需要动态传递一些参数,因为我的代码正在检查重复条目。我尝试编写一个预请求脚本,然后设置全局变量并尝试在我的请求中访问它。它不工作。 PFB,详细

预请求脚本

postman.setGlobalVariable("firstname", (text + parseInt(Math.random()*10000)).toString());
postman.setGlobalVariable("lastname", text + parseInt(Math.random()*10000));

body

{
“要求”:
{
"firstName":"{{firstname}}",
"middleName":"mani",
“姓氏”:“{{姓氏}}”
}
}

这里 firstName 被传递为 {{firstname}} 而不是随机字符串。

最佳答案

你可以通过添加来做到这一点

var rnd = Math.floor((Math.random() * 10000) + 1);
postman.setEnvironmentVariable("firstname", "fname"+rnd);
postman.setEnvironmentVariable("lastname", "lname"+rnd);

在预请求脚本部分。

然后添加
{ 
"firstName":"{{firstname}}",
"middleName":"mani",
"lastName":"{{lastname}}"
}

在 body 里。

我在 Postman 和 Newman 中都尝试过,并且完美地生成了一个随机的名字和姓氏。

关于dynamic - 如何在 postman 中传递动态json请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36005930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com