Zig
Zig
Upload File from String
See more Google Drive Examples
Uploads a text file where the contents of the file are contained in a string variable.See Google Drive Files: create for more details.
Also See Google Drive Multipart Upload for more details.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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 file.
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", "helloWorld.txt") catch {};
json.appendString("description", "A simple text file that says Hello World.") catch {};
json.appendString("mimeType", "text/plain") catch {};
rest.setMultipartBodyString(try json.emit(alloc)) catch {};
// The 2nd part is the file content.
// In this case, we'll upload a simple text file containing "Hello World!"
rest.setPartSelector("2");
rest.addHeader("Content-Type", "text/plain") catch {};
const file_contents = "Hello World!";
rest.setMultipartBodyString(file_contents) catch {};
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": "0B53Q6OSTWYoldmJ0Z3ZqT2x5MFk",
// "name": "Untitled",
// "mimeType": "text/plain"
// }
// Get the fileId:
std.debug.print("fileId: {s}\n", .{try json.stringOf(alloc, "id")});
}