Hello.

I use the Gmail API for receiving emails from the user's mail - everything is fine, everything works. To receive all letters, you need to send requests: for 100 letters - 100 requests. This is probably overloading the server, and I began to look for another way, but so far I have not found anything; decided turned to you.

Are there ways to get multiple emails in one request?

ID code for receiving letters:

var request = gapi.client.gmail.users.messages.list({ 'userId': "me", 'labelIds': labelID, 'maxResults': 10 }); 

The code for receiving the letters themselves:

 return gapi.client.gmail.users.messages.get({ 'userId': "me", 'id': messageID }); 

Thank you in advance!

1 answer 1

You're right, you can use batch to retrieve a list of messages as specified in https://developers.google.com/gmail/api/guides/sync under Full synchronization. But since it will be hard for you to recreate this in Javascript, I found the solution for finding SU here . In order to get the first three messages from the box there is a ready-made example:

 var rp = require('request-promise'); var batchUtils = require('google-api-batch-utils'); var createBatchBody = batchUtils.createBatchBody; var parseBatchResponse = batchUtils.parseBatchResponse; var BOUNDARY = 'example_boundary'; rp({ uri: 'https://www.googleapis.com/gmail/v1/users/me/messages', qs: { maxResults: 3 , fields: 'messages(id)'}, headers: { Authorization: 'Bearer {API_KEY}'}, json: true }).then(function(response) { var uris = response.messages.map(function(item) { return {uri: '/gmail/v1/users/me/messages/' + item.id, qs: { fields: 'snippet'}}; }); var batchBody = createBatchBody(uris, BOUNDARY); return rp({ method: 'POST', uri: 'https://www.googleapis.com/batch', headers: { Authorization: 'Bearer {API_KEY}' , 'Content-Type': 'multipart/mixed; boundary="' + BOUNDARY + '"' }, body: batchBody }); }).then(parseBatchResponse) .then(console.log.bind(console)); 
  • Thank you very much, but is there any documentation on how to use it? I didn’t understand anything honestly) - Hakob Shaghikyan