邮递员获取 Gmail 电子邮件 ID,遍历电子邮件 ID?

Postman Get Gmail Email ID, Iterate Through Email IDs?

对邮递员来说还很陌生,在链接 API 请求时,我从来没有完全专注于遍历消息 ID。

我有一个简单的GET请求如下

https://gmail.googleapis.com/gmail/v1/users/user/messages

这没有问题,并检索了我收件箱中的前 100 封邮件,我正在使用索引链接到另一个命令以打开该电子邮件并使用以下代码检查内容

Responsedata = JSON.parse(responseBody)
Value = Responsedata.messages[1].id
console.log(Value)
pm.environment.set("email_id", Value); 

我想不通的是如何在下一次迭代中迭代并增加 message[1].id 从 1 到 2?我从来没有完全关注递增变量,如果这就是你描述的方式的话。

JSON 响应正文

{
    "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
}

正在编辑第二部分,

然后我接受那个 email_id 并执行另一个 GET 请求

https://gmail.googleapis.com/gmail/v1/users/user/messages/{{email_id}}

然后打开并读取电子邮件的内容,这就是为什么我试图将 email_id 索引从 0 增加到 1,所以每次我 运行 API 链接到下一封电子邮件

我不确定我是否理解了这个问题,但为了增加 messages[1].id 的值,只需执行:

Responsedata.messages[1].id++

这将使 messages[1].id 值递增(加一)。

否则,如果你想迭代 Responsedata 对象中包含的超过 100 条消息,你可以这样做(因为消息是一个数组):

Responsedata = JSON.parse(responseBody)
for(let i = 0; i < Responsedata.messages.length; i++)
{
     Value = Responsedata.messages[i].id
     console.log(Value)
     // do wathever you want with each message
}
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"))

您可以将数组或任何内容存储为环境变量。 Postman 的工作方式与 Javascript.

一样

现在您在 emailID 变量中有了值。现在您可以使用 setNextRequest.

发送带有 id 请求的消息

如果您的请求名称和结构如下:

首先获取消息,然后获取 ID 为:

的消息

然后在 GET 消息中使用下面的预请求脚本,请求 ID 为:

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")
}

我建议遍历 ids 数组并使用 pm.sendRequest() 为每个 ID 发送 HTTP 请求。

const ids = pm.response.json().messages.map(item => item.id);

for (let id of ids) {
    const url = `https://gmail.googleapis.com/gmail/v1/users/user/messages/${id}`
    pm.sendRequest(url, (err, result) => {
        if(err) {
            console.log(err);
        } else {
            pm.test(`200 OK for id '${id}'`, () => pm.expect(result.code).to.eql(200));
        }
    })
}

我个人更喜欢使用pm.sendRequest(),因为:

  1. 您在脚本执行期间拥有更多控制权;
  2. postman.setNextRequest在您运行使用发送按钮请求时无效,它仅在您运行一个集合时生效。