Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

Inspect the MIME parts of a Gmail message

See more GMail REST API Examples

Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.

Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loToken
LOCAL loHttp
LOCAL loResp
LOCAL loReply
LOCAL loUrl
LOCAL loPaths
LOCAL loPart
LOCAL loChildPath
LOCAL p
LOCAL j

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("gmail-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not read gmail-token.json from the private working directory."
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
    ? "An OAuth access token is required."
    RELEASE loToken
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = loToken.StringOf("access_token")
loResp = CreateObject('Chilkat.HttpResponse')
loReply = CreateObject('Chilkat.JsonObject')
loUrl = CreateObject('Chilkat.StringBuilder')
loUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
loUrl.Append("GMAIL_MESSAGE_ID")
loUrl.Append("?format=full")
lnSuccess = loHttp.HttpNoBody("GET",loUrl.GetAsString(),loResp)
IF (loResp.StatusCode = 0) THEN
    ? "Gmail did not return an HTTP response."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    RELEASE loUrl
    CANCEL
ENDIF

IF ((lnSuccess <> 1) OR (loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    ? "Gmail HTTP " + STR(loResp.StatusCode)
    lnSuccess = loReply.Load(loResp.BodyStr)
    IF (lnSuccess = 1) THEN
        ? loReply.StringOf("error.message")
    ENDIF

    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    RELEASE loUrl
    CANCEL
ENDIF

lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a Gmail JSON response."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    RELEASE loUrl
    CANCEL
ENDIF

IF (loReply.JsonTypeOf("payload") <> 3) THEN
    ? "Expected a MIME payload."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    RELEASE loUrl
    CANCEL
ENDIF

*  Queue paths into the original JSON document so any MIME nesting depth is supported.
loPaths = CreateObject('Chilkat.StringTable')
lnSuccess = loPaths.Append("payload")
IF (lnSuccess <> 1) THEN
    ? "Could not queue the root part."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    RELEASE loUrl
    RELEASE loPaths
    CANCEL
ENDIF

loPart = CreateObject('Chilkat.JsonObject')
loChildPath = CreateObject('Chilkat.StringBuilder')
p = 0
DO WHILE (p < loPaths.Count)
    lnSuccess = loReply.ObjectOf2(loPaths.StringAt(p),loPart)
    IF (lnSuccess <> 1) THEN
        ? "Could not read a MIME part."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loReply
        RELEASE loUrl
        RELEASE loPaths
        RELEASE loPart
        RELEASE loChildPath
        CANCEL
    ENDIF

    ? "Part: " + loPart.StringOf("mimeType") + "  bytes: " + STR(loPart.IntOf("body.size"))
    IF (loPart.JsonTypeOf("filename") = 1) THEN
        IF (loPart.StringOfEquals("filename","",1) <> 1) THEN
            ? "Filename: " + loPart.StringOf("filename")
        ENDIF

    ENDIF

    IF (loPart.JsonTypeOf("body.attachmentId") = 1) THEN
        ? "Attachment ID: " + loPart.StringOf("body.attachmentId")
    ELSE
        IF (loPart.JsonTypeOf("body.data") = 1) THEN
            ? "Body data is available in this part."
        ENDIF

    ENDIF

    j = 0
    DO WHILE (j < loPart.SizeOfArray("parts"))
        loChildPath.SetString(loPaths.StringAt(p))
        loChildPath.Append(".parts[")
        loChildPath.AppendInt(j)
        loChildPath.Append("]")
        lnSuccess = loPaths.Append(loChildPath.GetAsString())
        IF (lnSuccess <> 1) THEN
            ? "Could not queue a child part."
            RELEASE loToken
            RELEASE loHttp
            RELEASE loResp
            RELEASE loReply
            RELEASE loUrl
            RELEASE loPaths
            RELEASE loPart
            RELEASE loChildPath
            CANCEL
        ENDIF

        j = j + 1
    ENDDO
    p = p + 1
ENDDO

RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loReply
RELEASE loUrl
RELEASE loPaths
RELEASE loPart
RELEASE loChildPath