Sample code for 30+ languages & platforms
Unicode C 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 Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkStringTableW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW token;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonObjectW reply;
    HCkStringBuilderW url;
    HCkStringTableW paths;
    HCkJsonObjectW part;
    HCkStringBuilderW childPath;
    int p;
    int j;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    token = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(token,L"gmail-token.json");
    if (success != TRUE) {
        wprintf(L"Could not read gmail-token.json from the private working directory.\n");
        CkJsonObjectW_Dispose(token);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"access_token") != 1) || (CkJsonObjectW_StringOfEquals(token,L"access_token",L"",TRUE) == TRUE)) {
        wprintf(L"An OAuth access token is required.\n");
        CkJsonObjectW_Dispose(token);
        return;
    }

    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(token,L"access_token"));
    resp = CkHttpResponseW_Create();
    reply = CkJsonObjectW_Create();
    url = CkStringBuilderW_Create();
    CkStringBuilderW_Append(url,L"https://gmail.googleapis.com/gmail/v1/users/me/messages/");
    CkStringBuilderW_Append(url,L"GMAIL_MESSAGE_ID");
    CkStringBuilderW_Append(url,L"?format=full");
    success = CkHttpW_HttpNoBody(http,L"GET",CkStringBuilderW_getAsString(url),resp);
    if (CkHttpResponseW_getStatusCode(resp) == 0) {
        wprintf(L"Gmail did not return an HTTP response.\n");
        CkJsonObjectW_Dispose(token);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        CkStringBuilderW_Dispose(url);
        return;
    }

    if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
        wprintf(L"Gmail HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
        success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
        if (success == TRUE) {
            wprintf(L"%s\n",CkJsonObjectW_stringOf(reply,L"error.message"));
        }

        CkJsonObjectW_Dispose(token);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        CkStringBuilderW_Dispose(url);
        return;
    }

    success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
    if (success != TRUE) {
        wprintf(L"Expected a Gmail JSON response.\n");
        CkJsonObjectW_Dispose(token);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        CkStringBuilderW_Dispose(url);
        return;
    }

    if (CkJsonObjectW_JsonTypeOf(reply,L"payload") != 3) {
        wprintf(L"Expected a MIME payload.\n");
        CkJsonObjectW_Dispose(token);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        CkStringBuilderW_Dispose(url);
        return;
    }

    //  Queue paths into the original JSON document so any MIME nesting depth is supported.
    paths = CkStringTableW_Create();
    success = CkStringTableW_Append(paths,L"payload");
    if (success != TRUE) {
        wprintf(L"Could not queue the root part.\n");
        CkJsonObjectW_Dispose(token);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        CkStringBuilderW_Dispose(url);
        CkStringTableW_Dispose(paths);
        return;
    }

    part = CkJsonObjectW_Create();
    childPath = CkStringBuilderW_Create();
    p = 0;
    while ((p < CkStringTableW_getCount(paths))) {
        success = CkJsonObjectW_ObjectOf2(reply,CkStringTableW_stringAt(paths,p),part);
        if (success != TRUE) {
            wprintf(L"Could not read a MIME part.\n");
            CkJsonObjectW_Dispose(token);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(reply);
            CkStringBuilderW_Dispose(url);
            CkStringTableW_Dispose(paths);
            CkJsonObjectW_Dispose(part);
            CkStringBuilderW_Dispose(childPath);
            return;
        }

        wprintf(L"Part: %s  bytes: %d\n",CkJsonObjectW_stringOf(part,L"mimeType"),CkJsonObjectW_IntOf(part,L"body.size"));
        if (CkJsonObjectW_JsonTypeOf(part,L"filename") == 1) {
            if (CkJsonObjectW_StringOfEquals(part,L"filename",L"",TRUE) != TRUE) {
                wprintf(L"Filename: %s\n",CkJsonObjectW_stringOf(part,L"filename"));
            }

        }

        if (CkJsonObjectW_JsonTypeOf(part,L"body.attachmentId") == 1) {
            wprintf(L"Attachment ID: %s\n",CkJsonObjectW_stringOf(part,L"body.attachmentId"));
        }
        else {
            if (CkJsonObjectW_JsonTypeOf(part,L"body.data") == 1) {
                wprintf(L"Body data is available in this part.\n");
            }

        }

        j = 0;
        while ((j < CkJsonObjectW_SizeOfArray(part,L"parts"))) {
            CkStringBuilderW_SetString(childPath,CkStringTableW_stringAt(paths,p));
            CkStringBuilderW_Append(childPath,L".parts[");
            CkStringBuilderW_AppendInt(childPath,j);
            CkStringBuilderW_Append(childPath,L"]");
            success = CkStringTableW_Append(paths,CkStringBuilderW_getAsString(childPath));
            if (success != TRUE) {
                wprintf(L"Could not queue a child part.\n");
                CkJsonObjectW_Dispose(token);
                CkHttpW_Dispose(http);
                CkHttpResponseW_Dispose(resp);
                CkJsonObjectW_Dispose(reply);
                CkStringBuilderW_Dispose(url);
                CkStringTableW_Dispose(paths);
                CkJsonObjectW_Dispose(part);
                CkStringBuilderW_Dispose(childPath);
                return;
            }

            j = j + 1;
        }

        p = p + 1;
    }



    CkJsonObjectW_Dispose(token);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(reply);
    CkStringBuilderW_Dispose(url);
    CkStringTableW_Dispose(paths);
    CkJsonObjectW_Dispose(part);
    CkStringBuilderW_Dispose(childPath);

    }