我想在 POSTMAN 中運行一個預請求腳本,該腳本向 UUID 生成器 API 發送請求并將 UUID 保存為變數。我可以在請求正文中呼叫它,因為那里需要它們。
最初,我有兩個單獨的請求 (1) 對 UUID Generator 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])
但是,我想簡化事情并在預請求部分插入 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])
我收到錯誤,否則我不會得到如果它是自己的請求(有效)
這讓我相信這可能是我擁有的預請求腳本的內部作業原理。
uj5u.com熱心網友回復:
你快到了。陣列以 [0] 開頭,您需要將回應宣告為 res,如放置在函式中:
pm.sendRequest({
url: "https://www.uuidgenerator.net/api/version1/3",
method: 'GET'
}, function(err, res){
pm.collectionVariables.set("UUID_1", res.text().split("\r\n")[0])
pm.collectionVariables.set("UUID_2", res.text().split("\r\n")[1])
pm.collectionVariables.set("UUID_3", res.text().split("\r\n")[2])
});
console.log(pm.response);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339005.html
標籤:javascript 爪哇 接口 要求 邮差
