邮递员获取 Gmail 电子邮件 ID,遍历电子邮件 ID?
对于邮递员来说是个新手,在链接 API 请求时,我从来没有完全考虑过遍历消息 ID.
我有一个简单的GET请求如下
然后在带有 id 请求的 GET 消息中使用以下预请求脚本:
let emailIDList = pm.environment.get("emailID");pm.environment.set("id", emailIDList.pop())pm.environment.set("emailID", emailIDList)if (emailIDList.length > 0) {postman.setNextRequest("获取带有 id 的消息")}
Fairly new to postman, I have never quite wrapped my head around iterating through message IDs when chaining API requests.
I have a simple GET request as follows
https://gmail.googleapis.com/gmail/v1/users/user/messages
This works no problem and retrieves the first 100 messages in my inbox, I am using the index to then chain to another command to open that email and inspect the contents with the following code
Responsedata = JSON.parse(responseBody)
Value = Responsedata.messages[1].id
console.log(Value)
pm.environment.set("email_id", Value);
What I can't figure out is how to iterate and increment that message[1].id from 1 to 2 on the next iteration? I have never quite wrapped my head around incrementing variables, if that's even how you would describe this.
JSON response body
{
"messages": [
{
"id": "175ec820de8d5815",
"threadId": "175eb6e43ed84e8a"
},
{
"id": "175ec63d888a91e4",
"threadId": "175eb6e43ed84e8a"
},
{
"id": "175ec4ac78ad714c",
"threadId": "175eb6e43ed84e8a"
},
{
"id": "175ec387d20eca9e",
"threadId": "175ec387d20eca9e"
},
{
"id": "175ec2e523051b86",
"threadId": "175eb6e43ed84e8a"
},
{
"id": "175ebec54123045d",
"threadId": "175ebec54123045d"
},
{
"id": "175ebc70bb0706ad",
"threadId": "175ebc70bb0706ad"
},
{
"id": "175ebb034f6b4e45",
"threadId": "175ebb034f6b4e45"
},
{
"id": "175eb9b037fb323d",
"threadId": "175eb9b037fb323d"
},
{
"id": "175eb92ff22a18e7",
"threadId": "175eb6e43ed84e8a"
},
{
"id": "175eb6e43ed84e8a",
"threadId": "175eb6e43ed84e8a"
},
{
"id": "175eb4a53485253e",
"threadId": "175eb4a53485253e"
},
{
"id": "175eb3863d4e91e6",
"threadId": "175eb3863d4e91e6"
},
{
"id": "175eb29ca100bd4b",
"threadId": "175eb29ca100bd4b"
},
{
"id": "175eb1e07c2a79c5",
"threadId": "175eb1e07c2a79c5"
},
{
"id": "175eaa249502f250",
"threadId": "175eaa249502f250"
},
{
"id": "175ea4c7a0f60ea8",
"threadId": "175ea4c7a0f60ea8"
},
{
"id": "175e966814c59a2f",
"threadId": "175e966814c59a2f"
},
{
"id": "175e90fb0b4e92c7",
"threadId": "175e90fb0b4e92c7"
},
{
"id": "175e902d58899476",
"threadId": "175e902d58899476"
},
{
"id": "175e901dd3bf8c1a",
"threadId": "175e901dd3bf8c1a"
},
{
"id": "175e8d3d2c2e2cdf",
"threadId": "175e8d3d2c2e2cdf"
},
{
"id": "175e88cc6625e8f7",
"threadId": "175e88cc6625e8f7"
},
{
"id": "175e8886780eb458",
"threadId": "175e8886780eb458"
},
{
"id": "175e8849c2580f35",
"threadId": "175e8849c2580f35"
},
{
"id": "175e860f338f99fd",
"threadId": "175e860f338f99fd"
},
{
"id": "175e826813c2512a",
"threadId": "175e826813c2512a"
},
{
"id": "175e8182b6d6fd4c",
"threadId": "175e8182b6d6fd4c"
},
{
"id": "175e811ddaa99f73",
"threadId": "175e811ddaa99f73"
},
{
"id": "175e804e4132a8f7",
"threadId": "175e804e4132a8f7"
},
{
"id": "175e7e5c4e038d51",
"threadId": "175e7e5c4e038d51"
},
{
"id": "175e7d67ca17d9a4",
"threadId": "175e7d67ca17d9a4"
},
{
"id": "175e7b10aae82ff4",
"threadId": "175e7b10aae82ff4"
},
{
"id": "175e7afe6336473d",
"threadId": "175e7afe6336473d"
},
{
"id": "175e7aab7ffa8caf",
"threadId": "175e7aab7ffa8caf"
},
{
"id": "175e7921f0aa2d37",
"threadId": "175e7921f0aa2d37"
},
{
"id": "175e7909f1cd7f21",
"threadId": "175e7909f1cd7f21"
},
{
"id": "175e77c5c38b3eea",
"threadId": "175e77c5c38b3eea"
},
{
"id": "175e767ba83807d9",
"threadId": "175e767ba83807d9"
},
{
"id": "175e766141962d76",
"threadId": "175e766141962d76"
},
{
"id": "175e74db2fbd3b95",
"threadId": "175e74db2fbd3b95"
},
{
"id": "175e740bcad276eb",
"threadId": "175e740bcad276eb"
},
{
"id": "175e72a7faeb4afe",
"threadId": "175e72a7faeb4afe"
},
{
"id": "175e70303a877f7f",
"threadId": "175e70303a877f7f"
},
{
"id": "175e6d1f9378159e",
"threadId": "175e6d1f9378159e"
},
{
"id": "175e6c4fd22ca201",
"threadId": "175e6c4fd22ca201"
},
{
"id": "175e6a219df346d2",
"threadId": "175e6a219df346d2"
},
{
"id": "175e69d61bbda68d",
"threadId": "175e69d61bbda68d"
},
{
"id": "175e69aca8a3f6b4",
"threadId": "175e69aca8a3f6b4"
},
{
"id": "175e671ee477728e",
"threadId": "175e671ee477728e"
},
{
"id": "175e669f68a6449d",
"threadId": "175e669f68a6449d"
},
{
"id": "175e660cc2fa4a2b",
"threadId": "175e660cc2fa4a2b"
},
{
"id": "175e644571920dcb",
"threadId": "175e644571920dcb"
},
{
"id": "175e63597c815898",
"threadId": "175e63597c815898"
},
{
"id": "175e62fdaa7db163",
"threadId": "175e62fdaa7db163"
},
{
"id": "175e607b5e54d4a4",
"threadId": "175e607b5e54d4a4"
},
{
"id": "175e5fadc78ecb2d",
"threadId": "175e5fadc78ecb2d"
},
{
"id": "175e5f62a75c7830",
"threadId": "175e5f62a75c7830"
},
{
"id": "175e5ce2503f0ed5",
"threadId": "175e5ce2503f0ed5"
},
{
"id": "175e59fb2a92ed04",
"threadId": "175e59fb2a92ed04"
},
{
"id": "175e58a770f24835",
"threadId": "175e58a770f24835"
},
{
"id": "175e47edfee50b0f",
"threadId": "175e47edfee50b0f"
},
{
"id": "175e479d78f5c82d",
"threadId": "175e479d78f5c82d"
},
{
"id": "175e443dc96e8ee1",
"threadId": "175e443dc96e8ee1"
},
{
"id": "175e4208a9f1d919",
"threadId": "175e4208a9f1d919"
},
{
"id": "175e37e1f1ee5661",
"threadId": "175e37e1f1ee5661"
},
{
"id": "175e37202f407a9e",
"threadId": "175e37202f407a9e"
},
{
"id": "175e35c496cb1b0f",
"threadId": "175e35c496cb1b0f"
},
{
"id": "175e35b4b9ba2206",
"threadId": "175e35b4b9ba2206"
},
{
"id": "175e32afb4abbdc7",
"threadId": "175e32afb4abbdc7"
},
{
"id": "175e31871a2314c9",
"threadId": "175e311c47b275c3"
},
{
"id": "175e283047fd2ac8",
"threadId": "175e283047fd2ac8"
},
{
"id": "175e26ef899364ca",
"threadId": "175e26ef899364ca"
},
{
"id": "175e25febda101eb",
"threadId": "175e25febda101eb"
},
{
"id": "175e251ed002c537",
"threadId": "175e251ed002c537"
},
{
"id": "175e21e252f693b7",
"threadId": "175e21e252f693b7"
},
{
"id": "175e2195e9789de5",
"threadId": "175e2195e9789de5"
},
{
"id": "175e203db4446415",
"threadId": "175e203db4446415"
},
{
"id": "175e1e2af08f9c8a",
"threadId": "175e1e2af08f9c8a"
},
{
"id": "175e1dc3064e0ed3",
"threadId": "175e1dc3064e0ed3"
},
{
"id": "175e18fa88a2e964",
"threadId": "175e18fa88a2e964"
},
{
"id": "175e168dd1c91145",
"threadId": "175e168dd1c91145"
},
{
"id": "175e15e336efaf17",
"threadId": "175e15e336efaf17"
},
{
"id": "175e12adedb64c27",
"threadId": "175e12adedb64c27"
},
{
"id": "175e1225c1cd0d92",
"threadId": "175e1225c1cd0d92"
},
{
"id": "175e115ded941cb2",
"threadId": "175e115ded941cb2"
},
{
"id": "175e0c0d4699ee7c",
"threadId": "175e0c0d4699ee7c"
},
{
"id": "175e007c6c86b017",
"threadId": "175e007c6c86b017"
},
{
"id": "175e0045b0ccfd3a",
"threadId": "175e0045b0ccfd3a"
},
{
"id": "175dfb57449a79a5",
"threadId": "175dfb57449a79a5"
},
{
"id": "175df2b0d97a4d3b",
"threadId": "175df2b0d97a4d3b"
},
{
"id": "175def4df0296793",
"threadId": "175def4df0296793"
},
{
"id": "175deb0de0d5f556",
"threadId": "175deb0de0d5f556"
},
{
"id": "175de5e09969f8c4",
"threadId": "175de5e09969f8c4"
},
{
"id": "175de5764bd6fb74",
"threadId": "175de5764bd6fb74"
},
{
"id": "175de36602c22133",
"threadId": "175de36602c22133"
},
{
"id": "175ddff4c43350c1",
"threadId": "175ddff4c43350c1"
},
{
"id": "175ddb71d738d02e",
"threadId": "175ddb71d738d02e"
},
{
"id": "175ddb286746873d",
"threadId": "175ddb286746873d"
},
{
"id": "175dd91c62c5ed50",
"threadId": "175dd91c62c5ed50"
}
],
"nextPageToken": "10494810510912086965",
"resultSizeEstimate": 618
}
Editing for the second portion,
I am then taking that email_id and doing another GET request
https://gmail.googleapis.com/gmail/v1/users/user/messages/{{email_id}}
Which then opens and reads the contents of the email, this is why I am trying to increment that email_id index from 0 to 1, so each time I run the API chain it moves onto the next email
解决方案let jsonData = pm.response.json();
let temp = []
jsonData.messages.forEach((b)=>{
temp.push(b.id)
})
pm.environment.set("emailID",temp)
console.log(pm.environment.get("emailID"))
You can store an array or anything as an environmental variable. Postman works just like Javascript.
Now you have value in the emailID
variable. Now you can send the message with id request using setNextRequest
.
If your request name and structure is like:
First getting messages then get a message with ID:
Then use below pre-request script in the GET message with id request:
let emailIDList = pm.environment.get("emailID");
pm.environment.set("id", emailIDList.pop())
pm.environment.set("emailID", emailIDList)
if (emailIDList.length > 0) {
postman.setNextRequest("get Message with id")
}
相关文章