Lua Requires Chilkat v11.0.0+
Lua
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.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local token = chilkat.JsonObject.new()
local success = token:load_file("gmail-token.json")
if not success then
print("Could not read gmail-token.json from the private working directory.")
return
end
if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
print("An OAuth access token is required.")
return
end
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = token:string_of("access_token")
local resp = chilkat.HttpResponse.new()
local reply = chilkat.JsonObject.new()
local url = chilkat.StringBuilder.new()
url:append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
url:append("GMAIL_MESSAGE_ID")
url:append("?format=full")
success = http:http_no_body("GET", url:get_as_string(), resp)
if resp.status_code == 0 then
print("Gmail did not return an HTTP response.")
return
end
if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
print("Gmail HTTP " .. resp.status_code)
success = reply:load(resp.body_str)
if success then
print(reply:string_of("error.message"))
end
return
end
success = reply:load(resp.body_str)
if not success then
print("Expected a Gmail JSON response.")
return
end
if reply:json_type_of("payload") ~= 3 then
print("Expected a MIME payload.")
return
end
-- Queue paths into the original JSON document so any MIME nesting depth is supported.
local paths = chilkat.StringTable.new()
success = paths:append("payload")
if not success then
print("Could not queue the root part.")
return
end
local part = chilkat.JsonObject.new()
local childPath = chilkat.StringBuilder.new()
local p = 0
while p < paths.count do
success = reply:object_of2(paths:string_at(p), part)
if not success then
print("Could not read a MIME part.")
return
end
print("Part: " .. part:string_of("mimeType") .. " bytes: " .. part:int_of("body.size"))
if part:json_type_of("filename") == 1 then
if not part:string_of_equals("filename", "", true) then
print("Filename: " .. part:string_of("filename"))
end
end
if part:json_type_of("body.attachmentId") == 1 then
print("Attachment ID: " .. part:string_of("body.attachmentId"))
elseif part:json_type_of("body.data") == 1 then
print("Body data is available in this part.")
end
local j = 0
while j < part:size_of_array("parts") do
childPath:set_string(paths:string_at(p))
childPath:append(".parts[")
childPath:append_int(j)
childPath:append("]")
success = paths:append(childPath:get_as_string())
if not success then
print("Could not queue a child part.")
return
end
j = j + 1
end
p = p + 1
end