Skip to main content
Best answer

Base64 decoded message gets split by characters


Sigurdur130
Forum|alt.badge.img+1

Hi everyone,

I'm building an action step to pull email bodies from a Gmail thread (the normal ‘find email’ action step finds an individual message, where I want every message in a given thread, and there’s no ‘find thread’ action step, so I’m making it). It's mostly working, but I'm running into a weird formatting issue when I try to handle the response.

I’m getting the message body in Base64, and when I decode it to a utf-8 string, it works, in that I get the messages, but they’re split by character. I’ve tried a few things to recombine the result, but it’s not working, and I don’t really know what I’m doing (I’m doing the code with ChatGPT. Sorry in advance for the ignorance I’m no doubt showing here).

Here's the result I'm getting:

{
  "0": "H",
  "1": "e",
  "2": "y",
  "3": " ",
  "4": "E",
  "5": "l",
  "6": "i",
  "7": "j",
  "8": "a",
  "9": "h",
  "10": "!",

etc.

Here's the code I'm using:

const options = {
  url: `https://www.googleapis.com/gmail/v1/users/me/threads/${bundle.inputData.Thread_ID}`,
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
};

return z.request(options)
  .then((response) => {
    const results = response.json;

    // Extracting email bodies from each message in the thread
    const messages = results.messages.map(message => {
      const parts = message.payload.parts || []; // Some messages might not have parts, handle that case
      const body = parts.find(part => part.body && part.body.data); // Find the part with body.data

      // If a body is found, base64 decode it
      if (body && body.body.data) {
        const decodedBody = Buffer.from(body.body.data, 'base64').toString('utf-8');
        return decodedBody;
      } else {
        return ''; // If no body is found, return an empty string
      }
    });

    return messages; // Return only the decoded email bodies
  });


As you can see, instead of getting a normal string like:
Hey Elijah!

...I'm getting an object where each character is stored separately with a numeric key.

Anyone seen this before? Or know a good pattern for handling?

Thanks so much in advance =)

Best answer by Troy Tessalone

Hi ​@Sigurdur130 

Feedback from AI…
 

 

 

return z.request(options)
  .then((response) => {
    const results = response.json;

    const messages = results.messages.map(message => {
      const parts = message.payload.parts || [];
      const body = parts.find(part => part.body && part.body.data);

      if (body && body.body.data) {
        const decodedBody = Buffer.from(body.body.data, 'base64').toString('utf-8');
        return decodedBody;
      } else {
        return '';
      }
    });

    // Return an array of strings, not an object
    return messages;
  });
return z.request(options)
  .then((response) => {
    const results = response.json;

    const messages = results.messages.map(message => {
      const parts = message.payload.parts || [];
      const body = parts.find(part => part.body && part.body.data);

      if (body && body.body.data) {
        const decodedBody = Buffer.from(body.body.data, 'base64').toString('utf-8');
        return decodedBody;
      } else {
        return '';
      }
    });

    return {
      fullThread: messages.join('\n---\n') // optional separator between messages
    };
  });

 

View original
Did this topic help you find an answer to your question?

2 replies

Troy Tessalone
Forum|alt.badge.img+14
  • Zapier Expert
  • 31615 replies
  • Answer
  • April 6, 2025

Hi ​@Sigurdur130 

Feedback from AI…
 

 

 

return z.request(options)
  .then((response) => {
    const results = response.json;

    const messages = results.messages.map(message => {
      const parts = message.payload.parts || [];
      const body = parts.find(part => part.body && part.body.data);

      if (body && body.body.data) {
        const decodedBody = Buffer.from(body.body.data, 'base64').toString('utf-8');
        return decodedBody;
      } else {
        return '';
      }
    });

    // Return an array of strings, not an object
    return messages;
  });
return z.request(options)
  .then((response) => {
    const results = response.json;

    const messages = results.messages.map(message => {
      const parts = message.payload.parts || [];
      const body = parts.find(part => part.body && part.body.data);

      if (body && body.body.data) {
        const decodedBody = Buffer.from(body.body.data, 'base64').toString('utf-8');
        return decodedBody;
      } else {
        return '';
      }
    });

    return {
      fullThread: messages.join('\n---\n') // optional separator between messages
    };
  });

 


Sigurdur130
Forum|alt.badge.img+1
  • Author
  • Beginner
  • 29 replies
  • April 7, 2025

Thanks for the help, Troy! The first AI suggestion was actually identical to the code I was already running, but the second worked! Thanks again =)


Reply