Zig
Zig
Google Drive Multipart Upload String
See more REST Examples
Demonstrates a file upload to Google Drive where the contents of the file are contained in a string variable.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 will upload a file to Google Drive.
// It requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const g_auth = try chilkat.AuthGoogle.init();
defer g_auth.deinit();
g_auth.setScope("https://www.googleapis.com/auth/drive");
g_auth.setSubEmailAddress("some.user@example.com");
g_auth.setExpireNumSeconds(3600);
// Obtain an access token as shown in one of the following examples:
// See Get Access Token using a Service Account JSON Key
// See Get Access Token using a P12 File
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 key)
try rest.setAuthGoogle(g_auth);
// A multipart upload to Google Drive needs a multipart/related Content-Type
try rest.addHeader("Content-Type", "multipart/related");
// Specify each part of the request.
// The 1st part is JSON with information about the file.
rest.setPartSelector("1");
try rest.addHeader("Content-Type", "application/json; charset=UTF-8");
const json = try chilkat.JsonObject.init();
defer json.deinit();
try json.addStringAt(-1, "title", "helloWorld.txt");
try json.addStringAt(-1, "description", "A simple text file that says Hello World.");
try json.addStringAt(-1, "mimeType", "text/plain");
try rest.setMultipartBodyString(try json.emit(alloc));
// The 2nd part is the file content.
// In this case, we'll upload a simple text file containing "Hello World!"
rest.setPartSelector("2");
try rest.addHeader("Content-Type", "text/plain");
try rest.setMultipartBodyString("Hello World!");
// POST https://www.googleapis.com/upload/drive/v2/files
const json_response = rest.fullRequestMultipart(alloc, "POST", "/upload/drive/v2/files?uploadType=multipart") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// Show the JSON response.
std.debug.print("Response Status Code: {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("Json Response: {s}\n", .{json_response});
}