Postman Obtenir un identifiant de messagerie Gmail, itérer via des identifiants de messagerie?

Nov 21 2020

Assez nouveau pour facteur, je n'ai jamais tout à fait pensé à parcourir les ID de message lors du chaînage des demandes d'API.

J'ai une simple demande GET comme suit

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

Cela fonctionne sans problème et récupère les 100 premiers messages de ma boîte de réception, j'utilise l'index pour ensuite chaîner une autre commande pour ouvrir cet e-mail et inspecter le contenu avec le code suivant

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

Ce que je ne peux pas comprendre, c'est comment itérer et incrémenter ce message [1] .id de 1 à 2 lors de la prochaine itération? Je n'ai jamais vraiment réfléchi à l'incrémentation des variables, si c'est même ainsi que vous décririez cela.

Corps de la réponse 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
}

Montage pour la deuxième partie,

Je prends alors cet email_id et fais une autre requête GET

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

Qui ouvre ensuite et lit le contenu de l'e-mail, c'est pourquoi j'essaie d'incrémenter cet index email_id de 0 à 1, donc chaque fois que j'exécute la chaîne d'API, il passe au prochain e-mail

Réponses

2 PDHide Nov 22 2020 at 05:45
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"))

Vous pouvez stocker un tableau ou quoi que ce soit en tant que variable d'environnement. Postman fonctionne comme javascript

Maintenant, vous avez de la valeur dans emailID varaible. MAINTENANT, vous pouvez envoyer le message avec la demande d'identifiant en utilisant setNExtRequest

Si le nom et la structure de votre requête sont comme:

Commencez par recevoir les messages, puis recevez le message avec l'ID:

Ensuite, utilisez ci-dessous le script pre reqeust dans le message get avec la demande d'identifiant:

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

DorianN. Nov 21 2020 at 08:19

Je ne suis pas sûr d'avoir bien compris la question, mais pour augmenter la valeur de messages[1].id, il suffit de faire:

Responsedata.messages[1].id++

Cela incrémentera (de un) la messages[1].idvaleur.

Sinon , si vous souhaitez parcourir plus de 100 messages contenus dans l'objet Responsedata, vous pouvez faire (puisque les messages est un tableau):

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
}