Zig
Zig
Create a New GMail Label
See more GMail REST API Examples
Demonstrates how to create a new GMail label.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
http.setAuthToken("GMAIL-ACCESS-TOKEN");
const user_id = "me";
http.setUrlVar("userId", user_id) catch {};
// Create the JSON to be sent in the HTTP request body.
// The name of the new label is "questions".
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("name", "questions") catch {};
json.updateString("labelListVisibility", "labelShow") catch {};
json.updateString("messageListVisibility", "show") catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// The JSON contains this:
// {
// "name": "questions",
// "labelListVisibility": "labelShow",
// "messageListVisibility": "show"
// }
const url = "https://www.googleapis.com/gmail/v1/users/{$userId}/labels";
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", url, json, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
std.debug.print("status = {d}\n", .{resp.getStatusCode()});
// A 200 response status indicate success.
if (resp.getStatusCode() != 200) {
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
// A successful repsonse contains JSON that looks like this:
// {
// "id": "Label_43",
// "name": "questions",
// "messageListVisibility": "show",
// "labelListVisibility": "labelShow"
// }
std.debug.print("response body:\n", .{});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
std.debug.print("GMail label created!\n", .{});
}