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

Objective-C
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.h>
#import <CkoStringTable.h>

BOOL success = NO;

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

CkoJsonObject *token = [[CkoJsonObject alloc] init];
success = [token LoadFile: @"gmail-token.json"];
if (success != YES) {
    NSLog(@"%@",@"Could not read gmail-token.json from the private working directory.");
    return;
}

if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"An OAuth access token is required.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [token StringOf: @"access_token"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://gmail.googleapis.com/gmail/v1/users/me/messages/"];
[url Append: @"GMAIL_MESSAGE_ID"];
[url Append: @"?format=full"];
success = [http HttpNoBody: @"GET" url: [url GetAsString] response: resp];
if ([resp.StatusCode intValue] == 0) {
    NSLog(@"%@",@"Gmail did not return an HTTP response.");
    return;
}

if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
    NSLog(@"%@%d",@"Gmail HTTP ",[resp.StatusCode intValue]);
    success = [reply Load: resp.BodyStr];
    if (success == YES) {
        NSLog(@"%@",[reply StringOf: @"error.message"]);
    }

    return;
}

success = [reply Load: resp.BodyStr];
if (success != YES) {
    NSLog(@"%@",@"Expected a Gmail JSON response.");
    return;
}

if ([[reply JsonTypeOf: @"payload"] intValue] != 3) {
    NSLog(@"%@",@"Expected a MIME payload.");
    return;
}

//  Queue paths into the original JSON document so any MIME nesting depth is supported.
CkoStringTable *paths = [[CkoStringTable alloc] init];
success = [paths Append: @"payload"];
if (success != YES) {
    NSLog(@"%@",@"Could not queue the root part.");
    return;
}

CkoJsonObject *part = [[CkoJsonObject alloc] init];
CkoStringBuilder *childPath = [[CkoStringBuilder alloc] init];
int p = 0;
while ((p < [paths.Count intValue])) {
    success = [reply ObjectOf2: [paths StringAt: [NSNumber numberWithInt: p]] jsonObj: part];
    if (success != YES) {
        NSLog(@"%@",@"Could not read a MIME part.");
        return;
    }

    NSLog(@"%@%@%@%d",@"Part: ",[part StringOf: @"mimeType"],@"  bytes: ",[[part IntOf: @"body.size"] intValue]);
    if ([[part JsonTypeOf: @"filename"] intValue] == 1) {
        if ([part StringOfEquals: @"filename" value: @"" caseSensitive: YES] != YES) {
            NSLog(@"%@%@",@"Filename: ",[part StringOf: @"filename"]);
        }

    }

    if ([[part JsonTypeOf: @"body.attachmentId"] intValue] == 1) {
        NSLog(@"%@%@",@"Attachment ID: ",[part StringOf: @"body.attachmentId"]);
    }
    else {
        if ([[part JsonTypeOf: @"body.data"] intValue] == 1) {
            NSLog(@"%@",@"Body data is available in this part.");
        }

    }

    int j = 0;
    while ((j < [[part SizeOfArray: @"parts"] intValue])) {
        [childPath SetString: [paths StringAt: [NSNumber numberWithInt: p]]];
        [childPath Append: @".parts["];
        [childPath AppendInt: [NSNumber numberWithInt: j]];
        [childPath Append: @"]"];
        success = [paths Append: [childPath GetAsString]];
        if (success != YES) {
            NSLog(@"%@",@"Could not queue a child part.");
            return;
        }

        j = j + 1;
    }

    p = p + 1;
}