Gmail API - how to parse message body data correctly?

I use the new Gmail API and am absolutely fixated on how to properly handle the encoding of the [body] [data] part in Ruby / Rails for a text / plain message and text / html message.

Say data = part of an encoded message.

A call Base64.decode64(data).unpack("M")on it returns a US-ASCII encoded text body with a large number of missing characters displayed on the web page.

The call Base64.decode64(data).encode('UTF-8')causes a conversion error from US-ASCII to UTF-8

But if I do Base64.decode64(data).encode('UTF-8', {:invalid => :replace, :undef => :replace, :replace => '?'}), I still see a ton of question marks.

Can someone point me in the right direction how to get the message body to be correctly encoded and displayed in UTF-8?

The formatting of the JSON email response is as follows:

"parts": [
   {
    "partId": "0",
    "mimeType": "text/plain",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "text/plain; charset=UTF-8"
     },
     {
      "name": "Content-Transfer-Encoding",
      "value": "quoted-printable"
+4
source share
3 answers

Use Base64.urlsafe_decode64to decode the message body.

+4
source
var base64toUTF8 = function base64toUTF8(str,urlsafe) {
  if(urlsafe) {
    str = str.replace(/_/g,"/");
    str = str.replace(/-/g,"+");
  }
  if(typeof window) {
    return decodeURIComponent(escape(window.atob( str )));
  }
  else if(typeof module !== 'undefined' && module.exports) {
    return new Buffer("SGVsbG8gV29ybGQ=", 'base64').toString('utf8');
  }
};

just need to replace base64 encoded characters "-" with "+" and "_" with "/"

+2
source

, , :

var base64toUTF8 = function base64toUTF8(str, urlsafe) {
  if (urlsafe) {
    str = str.replace(/_/g,"/");
    str = str.replace(/-/g,"+");
  }
  return new Buffer(str, 'base64').toString('utf8');
};

window vs module, , NodeJS str, "Hello World!"

0
source

Source: https://habr.com/ru/post/1546503/


All Articles