【问题标题】:Postman Get Gmail Email ID, Iterate Through Email IDs?邮递员获取 Gmail 电子邮件 ID,遍历电子邮件 ID?
【发布时间】:2021-03-04 09:35:33
【问题描述】:

对于邮递员来说是个新手,在链接 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 链时它都会移动到下一封电子邮件

【问题讨论】:

    标签: javascript postman gmail-api


    【解决方案1】:
    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 的消息:

    然后在带有 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("get Message with id")
    }
    

    【讨论】:

    • 我需要处理我的 javascript,我不在我的领域。正如您所描述的,这个解决方案似乎将所有 ID 放在一个数组中,假设在我下一步调用“emailID”时,我可以指向数组的索引,然后增加该索引以随机浏览每封电子邮件?在我看来,该程序将遍历请求 100 次,每次选择下一个电子邮件 ID 并根据下一个获取请求中的一组值检查它。假设对整个列表执行获取请求更容易,然后遍历该数组?
    • @Josh 对请求感兴趣的地方在哪里?它正在迭代响应。
    • 发送请求,获取响应,将响应中的所有电子邮件 ID 存储到数组中。现在你可以用 id 做你想做的事
    • @Josh 添加了完整的解决方案,现在我们将使用每个 email_id 值发送第一个请求一次,第二个请求 100 次
    • 非常感谢。这正是我想要做的。这现在允许电子邮件 ID 增加,然后存储在 id var 中。我认为让我失望的(在多件事中)是请求前部分,我一直在测试它,忘记了在请求之前运行的部分。我将学习一些 Javascript 技能,因为我缺乏这些技能。巨大的帮助。
    【解决方案2】:

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

    Responsedata.messages[1].id++

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

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

    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
    }
    

    【讨论】:

    • 嗨@Dorian,感谢您的回复。我以为我试过了,我回去将它编辑为Responsedata = JSON.parse(responseBody) Value = Responsedata.messages[1].id++ console.log(Value) pm.environment.set("email_id", Value); ,但在下一次迭代时它仍然没有在我的收件箱中获取下一个 ID。也许我仍然在执行这个错误?
    • 能否提供一个 responseBody json 内容的样本?这将有助于我们理解问题。另外,为了您的信息,Value = Responsedata.messages[1].id++,如果我没记错的话,会将Responsedata.messages[1].id 分配给“Value”变量,然后才将Responsedata.messages[1].id 值加一。
    • 刚刚用响应正文更新了原始问题。谢谢!我是否需要将其设为环境变量或其他任何东西?如前所述,我是邮递员的新手......但变量不会总是默认返回索引 [1],因为它会重新运行每次迭代吗?
    【解决方案3】:

    我建议遍历 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 在您使用发送按钮运行请求时无效,它仅在您运行集合时生效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-15
      • 2018-11-03
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多