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

Go
    success := false

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

    token := chilkat.NewJsonObject()
    success = token.LoadFile("gmail-token.json")
    if success != true {
        fmt.Println("Could not read gmail-token.json from the private working directory.")
        token.DisposeJsonObject()
        return
    }

    if (token.JsonTypeOf("access_token") != 1) || (token.StringOfEquals("access_token","",true) == true) {
        fmt.Println("An OAuth access token is required.")
        token.DisposeJsonObject()
        return
    }

    http := chilkat.NewHttp()
    http.SetRequireSslCertVerify(true)
    http.SetAuthToken(token.StringOf("access_token"))
    resp := chilkat.NewHttpResponse()
    reply := chilkat.NewJsonObject()
    url := chilkat.NewStringBuilder()
    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.GetAsString(),resp)
    if resp.StatusCode() == 0 {
        fmt.Println("Gmail did not return an HTTP response.")
        token.DisposeJsonObject()
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        reply.DisposeJsonObject()
        url.DisposeStringBuilder()
        return
    }

    if (success != true) || (resp.StatusCode() < 200) || (resp.StatusCode() >= 300) {
        fmt.Println("Gmail HTTP ", resp.StatusCode())
        success = reply.Load(resp.BodyStr())
        if success == true {
            fmt.Println(*reply.StringOf("error.message"))
        }

        token.DisposeJsonObject()
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        reply.DisposeJsonObject()
        url.DisposeStringBuilder()
        return
    }

    success = reply.Load(resp.BodyStr())
    if success != true {
        fmt.Println("Expected a Gmail JSON response.")
        token.DisposeJsonObject()
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        reply.DisposeJsonObject()
        url.DisposeStringBuilder()
        return
    }

    if reply.JsonTypeOf("payload") != 3 {
        fmt.Println("Expected a MIME payload.")
        token.DisposeJsonObject()
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        reply.DisposeJsonObject()
        url.DisposeStringBuilder()
        return
    }

    //  Queue paths into the original JSON document so any MIME nesting depth is supported.
    paths := chilkat.NewStringTable()
    success = paths.Append("payload")
    if success != true {
        fmt.Println("Could not queue the root part.")
        token.DisposeJsonObject()
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        reply.DisposeJsonObject()
        url.DisposeStringBuilder()
        paths.DisposeStringTable()
        return
    }

    part := chilkat.NewJsonObject()
    childPath := chilkat.NewStringBuilder()
    p := 0
    for (p < paths.Count()) {
        success = reply.ObjectOf2(*paths.StringAt(p),part)
        if success != true {
            fmt.Println("Could not read a MIME part.")
            token.DisposeJsonObject()
            http.DisposeHttp()
            resp.DisposeHttpResponse()
            reply.DisposeJsonObject()
            url.DisposeStringBuilder()
            paths.DisposeStringTable()
            part.DisposeJsonObject()
            childPath.DisposeStringBuilder()
            return
        }

        fmt.Println("Part: ", *part.StringOf("mimeType"), "  bytes: ", part.IntOf("body.size"))
        if part.JsonTypeOf("filename") == 1 {
            if part.StringOfEquals("filename","",true) != true {
                fmt.Println("Filename: ", *part.StringOf("filename"))
            }

        }

        if part.JsonTypeOf("body.attachmentId") == 1 {
            fmt.Println("Attachment ID: ", *part.StringOf("body.attachmentId"))
        } else {
            if part.JsonTypeOf("body.data") == 1 {
                fmt.Println("Body data is available in this part.")
            }

        }

        j := 0
        for (j < part.SizeOfArray("parts")) {
            childPath.SetString(*paths.StringAt(p))
            childPath.Append(".parts[")
            childPath.AppendInt(j)
            childPath.Append("]")
            success = paths.Append(*childPath.GetAsString())
            if success != true {
                fmt.Println("Could not queue a child part.")
                token.DisposeJsonObject()
                http.DisposeHttp()
                resp.DisposeHttpResponse()
                reply.DisposeJsonObject()
                url.DisposeStringBuilder()
                paths.DisposeStringTable()
                part.DisposeJsonObject()
                childPath.DisposeStringBuilder()
                return
            }

            j = j + 1
        }

        p = p + 1
    }


    token.DisposeJsonObject()
    http.DisposeHttp()
    resp.DisposeHttpResponse()
    reply.DisposeJsonObject()
    url.DisposeStringBuilder()
    paths.DisposeStringTable()
    part.DisposeJsonObject()
    childPath.DisposeStringBuilder()