Zig
Zig
Upload String to Dropbox
See more Dropbox Examples
Uploads a string to a file on Dropbox. This example can upload content up to 150MB, assuming a 150MB string fits in memory for your app. Larger files must be uploaded with an upload session (upload_session/start).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.
// A Dropbox access token should have been previously obtained.
// Dropbox access tokens do not expire.
// See Dropbox Access Token.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to Dropbox
rest.connect("content.dropboxapi.com", 443, true, true) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// Add request headers.
rest.addHeader("Content-Type", "application/octet-stream") catch {};
rest.addHeader("Authorization", "Bearer DROPBOX_ACCESS_TOKEN") catch {};
// The upload "parameters" contained in JSON passed in an HTTP request header.
// This is the JSON to be added in this example:
// {
// "path": "/jack.txt",
// "mode": "add",
// "autorename": true,
// "mute": false
// }
// This will be the content of the file created in Dropbox.
const content = "All work and no play makes Jack a dull boy";
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.appendString("path", "/jack.txt") catch {};
json.appendString("mode", "add") catch {};
json.appendBool("autorename", true) catch {};
json.appendBool("mute", false) catch {};
rest.addHeader("Dropbox-API-Arg", try json.emit(alloc)) catch {};
// Do the upload. The URL is https://content.dropboxapi.com/2/files/upload.
// We already connected to content.dropboxapi.com using TLS (i.e. HTTPS),
// so now we only need to specify the path "/2/files/upload".
const response_str = rest.fullRequestString(alloc, "POST", "/2/files/upload", content) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// When successful, Dropbox responds with a 200 response code.
if (rest.getResponseStatusCode() != 200) {
// Examine the request/response to see what happened.
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 body (if any): {s}\n", .{response_str});
std.debug.print("---\n", .{});
std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
return;
}
std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
// The response is JSON.
const json_resp = try chilkat.JsonObject.init();
defer json_resp.deinit();
json_resp.setEmitCompact(false);
json_resp.load(response_str) catch {};
// Show the JSON response.
std.debug.print("{s}\n", .{try json_resp.emit(alloc)});
// Returns JSON that looks like this:
// {
// "name": "jack.txt",
// "path_lower": "/jack.txt",
// "path_display": "/jack.txt",
// "id": "id:yqx4-tE_NKAAAAAAAAAAAQ",
// "client_modified": "2016-06-02T20:42:11Z",
// "server_modified": "2016-06-02T20:42:11Z",
// "rev": "8482db15f",
// "size": 42
// }
// Sample code to get data from the JSON response:
const size = json_resp.intOf("size");
std.debug.print("size = {d}\n", .{size});
const rev = try json_resp.stringOf(alloc, "rev");
std.debug.print("rev = {s}\n", .{rev});
const client_modified = try json_resp.stringOf(alloc, "client_modified");
const ckdt = try chilkat.DateTime.init();
defer ckdt.deinit();
ckdt.setFromTimestamp(client_modified) catch {};
const b_local_time = true;
const dt = try chilkat.DtObj.init();
defer dt.deinit();
ckdt.toDtObj(b_local_time, dt);
std.debug.print("{d}/{d}/{d} {d}:{d}\n", .{ dt.getDay(), dt.getMonth(), dt.getYear(), dt.getHour(), dt.getMinute() });
}