Reputation: 103
I am new to RESTful API as well as dealing with MIME content. I am trying to write an app to retrieve messages from Gmail mailbox for further processing.
I use Google Developers API reference as a tool to get myself familiar with the API. Using the "Try it" feature in the following GMail API reference
https://developers.google.com/gmail/api/v1/reference/users/messages/get
I retrieve a simple test message which just has 2 lines of text. The first line contains simple ASCII while the second line contains some Unicode text.
This the first line of body text in English only
This is second line, 中文睇到嗎 , could you see those Chinese
and the following response is obtained
"parts": [
{
"partId": "0",
"mimeType": "text/plain",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/plain; charset=UTF-8"
},
{
"name": "Content-Transfer-Encoding",
"value": "quoted-printable"
}
],
"body": {
"size": 120,
"data": "VGhpcyB0aGUgZmlyc3QgbGluZSBvZiBib2R5IHRleHQgaW4gRW5nbGlzaCBvbmx5DQoNClRoaXMgaXMgc2Vjb25kIGxpbmUsIOS4reaWh-edh-WIsOWXjiAsIGNvdWxkIHlvdSBzZWUgdGhvc2UgQ2hpbmVzZQ0K"
}
},
{
"partId": "1",
"mimeType": "text/html",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
},
{
"name": "Content-Transfer-Encoding",
"value": "quoted-printable"
}
],
"body": {
"size": 354,
"data": "PGRpdiBkaXI9Imx0ciI-PGRpdiBjbGFzcz0iZ21haWxfZGVmYXVsdCIgc3R5bGU9ImZvbnQtZmFtaWx5OnRhaG9tYSxzYW5zLXNlcmlmIj5UaGlzIHRoZSBmaXJzdCBsaW5lIG9mIGJvZHkgdGV4dCBpbiBFbmdsaXNoIG9ubHk8L2Rpdj48ZGl2IGNsYXNzPSJnbWFpbF9kZWZhdWx0IiBzdHlsZT0iZm9udC1mYW1pbHk6dGFob21hLHNhbnMtc2VyaWYiPjxicj48L2Rpdj48ZGl2IGNsYXNzPSJnbWFpbF9kZWZhdWx0IiBzdHlsZT0iZm9udC1mYW1pbHk6dGFob21hLHNhbnMtc2VyaWYiPlRoaXMgaXMgc2Vjb25kIGxpbmUsIOS4reaWh-edh-WIsOWXjiAsIGNvdWxkIHlvdSBzZWUgdGhvc2UgQ2hpbmVzZTwvZGl2PjwvZGl2Pg0K"
}
}
]
As you can see, while the "content-transfer-encoding" is specified as "quoted-printable" the actual data returned is in base64url encoding.
I get the same 64baseurl encoded response when I use my test script to retrieve the message via GMail RESTful API.
So my questions is why there is a disagreement between the "content-transfer-encoding" in header against the actual encoded response? Did I overlook something or there are some additional parameters/request to make it correct?
Thanks
Edit: based on the comment from @EricDeFriez
If I base64url decode the 'data' field
"VGhpcyB0aGUgZmlyc3QgbGluZSBvZiBib2R5IHRleHQgaW4gRW5nbGlzaCBvbmx5DQoNClRoaXMgaXMgc2Vjb25kIGxpbmUsIOS4reaWh-edh-WIsOWXjiAsIGNvdWxkIHlvdSBzZWUgdGhvc2UgQ2hpbmVzZQ0K"
It seems the result is already the original utf8 coded text :
This the first line of body text in English only
This is second line, 中文睇到嗎 , could you see those Chinese
If it was 'quoted-printable' encoded, the result of base64url decode should look like this :
This the first line of body text in English only
This is second line, =E4=B8=AD=E6=96=87=E7=9D=87=E5=88=B0=E5=97=8E , could =
you see those Chinese
It seems that the 'quoted-printable' CTE header in the JSON response is not correct. The data field is always base64url encoded regardless what is specified in CTE header.
Do you know the reason?
Upvotes: 2
Views: 1119
Reputation: 7159
The "data" field isn't necessarily safe for transport natively over JSON so it's always base64url encoded. Once you base64url decode 'data' it should match the CTE header.
Upvotes: 2