【发布时间】:2021-12-12 02:59:58
【问题描述】:
我想在 POSTMAN 中运行一个预请求脚本,该脚本向 UUID 生成器 API 发送请求并将 UUID 保存为变量。我可以在请求正文中调用它,因为那里需要它们。
最初,我有两个单独的请求 (1) 一个对 UUID 生成器 API 的请求 (2) 另一个对我的私有 API 的请求。我可以将 3 个 UUID 设置为变量,并根据对我的私有 API 的请求将它们调用到正文中,这没问题,并且可以正常工作。像这样:
GET https://www.uuidgenerator.net/api/version1/3
pm.collectionVariables.set("UUID_1", pm.response.text().split(" ")[1])
pm.collectionVariables.set("UUID_2", pm.response.text().split(" ")[2])
pm.collectionVariables.set("UUID_3", pm.response.text().split(" ")[3])
**Response:**
1. f3600940-3684-11ec-8d3d-0242ac130003
2. f3600b70-3684-11ec-8d3d-0242ac130003
3. f3600c6a-3684-11ec-8d3d-0242ac130003
**Variables in body of next Request:**
"data": {
"uuid": "{{UUID_1}}",
"uuid2": "{{UUID_2}}",
"uuid3": "{{UUID_3}}",
旁注:我想出如何从响应中获取个性化原始文本数据的唯一方法是使用 .split 并且数据是一行,pm.response.text().split(" ")[1])
但是,我想简化一些事情并在 Pre-Request 部分插入 UUID 生成器请求,因此我不需要两个单独的请求来完成这项工作。但是,当插入与上述先前请求相同的信息来设置这些变量时,它不起作用。
到目前为止,我的预请求脚本给出了一个错误(如下):
pm.sendRequest({
url: "https://www.uuidgenerator.net/api/version1/3",
method: 'GET'
}, function (err, res) {
pm.collectionVariables.set("UUID_1", pm.response.text().split(" ")[1])
pm.collectionVariables.set("UUID_2", pm.response.text().split(" ")[2])
pm.collectionVariables.set("UUID_3", pm.response.text().split(" ")[3])
});
POSTMAN Error:
There was an error in evaluating the Pre-request Script:Error: Cannot read property 'text' of undefined
基本上我在预请求脚本中的collectionVariables.set 中尝试的任何不同变体......
pm.collectionVariables.set("UUID_1", pm.response.text().split(" ")[1])
OR
pm.collectionVariables.set("UUID_2", pm.response.split(" ")[2])
OR
pm.collectionVariables.set("UUID_3", pm.response.[3])
我收到错误,如果它是自己的请求(有效),否则我不会收到错误
这让我相信这可能是我拥有的预请求脚本的内部工作原理。
【问题讨论】:
-
只是问,因为我不知道使用该 uuid 生成器背后的上下文 - 你能不使用
pm.variables.replaceIn('{{$guid}}')来获取 uuid 吗? -
这是我要研究的东西。 @丹尼丹顿
标签: javascript java api request postman