Zig
Zig
Download File from Dropbox into a String Variable
See more Dropbox Examples
Demonstrates how to download a file from Dropbox directly into 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 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("Authorization", "Bearer DROPBOX_ACCESS_TOKEN") catch {};
// The download "parameters" are contained in JSON passed in an HTTP request header.
// This is the JSON indicating the file to be downloaded:
// {
// "path": "/jack.txt",
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.appendString("path", "/jack.txt") catch {};
rest.addHeader("Dropbox-API-Arg", try json.emit(alloc)) catch {};
// The content of the file on Dropbox is returned.
const file_content = rest.fullRequestNoBody(alloc, "POST", "/2/files/download") 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", .{file_content});
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;
}
// Show the file content that was downloaded:
std.debug.print("{s}\n", .{file_content});
std.debug.print("----\n", .{});
// Information about the downloaded file is also available as JSON in a response header.
// The "dropbox-api-result" response header contains the information. For example:
const api_result = try rest.responseHdrByName(alloc, "dropbox-api-result");
std.debug.print("{s}\n", .{api_result});
// In this case, the pretty-formatted dropbox-api-result JSON 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
// }
// Load the JSON, pretty-print it, and demonstrate how to get some values...
const json_result = try chilkat.JsonObject.init();
defer json_result.deinit();
json_result.setEmitCompact(false);
json_result.load(api_result) catch {};
// Show the JSON pretty-printed...
std.debug.print("{s}\n", .{try json_result.emit(alloc)});
// Sample code to get data from the JSON response:
const size = json_result.intOf("size");
std.debug.print("size = {d}\n", .{size});
const rev = try json_result.stringOf(alloc, "rev");
std.debug.print("rev = {s}\n", .{rev});
const client_modified = try json_result.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() });
}