Sample code for 30+ languages & platforms
Zig

Create Folder

See more Google Drive Examples

In the Drive API, a folder is essentially a file — one identified by the special folder MIME type application/vnd.google-apps.folder

See Google Drive Files: create for more details.

Also See Working with Folders for more details.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // Creating a folder is technically an upload of a 0-length file
    // having a MIME type of "application/vnd.google-apps.folder"

    // It requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example uses a previously obtained access token having permission for the
    // Google Drive scope.

    const g_auth = try chilkat.AuthGoogle.init();
    defer g_auth.deinit();
    g_auth.setAccessToken("GOOGLE-DRIVE-ACCESS-TOKEN");

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect using TLS.
    const b_auto_reconnect = true;
    try rest.connect("www.googleapis.com", 443, true, b_auto_reconnect);

    // Provide the authentication credentials (i.e. the access token)
    rest.setAuthGoogle(g_auth) catch {};

    // A multipart upload to Google Drive needs a multipart/related Content-Type
    rest.addHeader("Content-Type", "multipart/related") catch {};

    // Specify each part of the request.

    // The 1st part is JSON with information about the folder.
    rest.setPartSelector("1");
    rest.addHeader("Content-Type", "application/json; charset=UTF-8") catch {};

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.appendString("name", "testFolder") catch {};
    json.appendString("description", "A folder to contain test files.") catch {};
    json.appendString("mimeType", "application/vnd.google-apps.folder") catch {};
    rest.setMultipartBodyString(try json.emit(alloc)) catch {};

    // The 2nd part would be the file content.
    // Since this is a folder, skip the 2nd part entirely and go straight to the upload..

    const json_response = rest.fullRequestMultipart(alloc, "POST", "/upload/drive/v3/files?uploadType=multipart") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // A successful response will have a status code equal to 200.
    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response JSON: {s}\n", .{json_response});
        return;
    }

    // Show the JSON response.
    json.load(json_response) catch {};

    // Show the full JSON response.
    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // A successful response looks like this:
    // {
    //   "kind": "drive#file",
    //   "id": "0B53Q6OSTWYolY2tPU1BnYW02T2c",
    //   "name": "testFolder",
    //   "mimeType": "application/vnd.google-apps.folder"
    // }

    // Get the fileId:
    std.debug.print("fileId: {s}\n", .{try json.stringOf(alloc, "id")});
}