Sample code for 30+ languages & platforms
Zig

Download File from Dropbox (Streaming)

See more Dropbox Examples

Downloads a file from Dropbox, streaming it directly to a file in the filesystem.

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();

    // 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": "/Homework/lit/hamlet.xml",
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.appendString("path", "/Homework/lit/hamlet.xml") catch {};
    rest.addHeader("Dropbox-API-Arg", try json.emit(alloc)) catch {};

    // Setup a file stream for the download
    const file_stream = try chilkat.Stream.init();
    defer file_stream.deinit();
    file_stream.setSinkFile("qa_output/hamletFromDropbox.xml");

    // Indicate that the call to FullRequestNoBody should send the response body
    // to fileStream if the response status code is 200.
    // If a non-success response status code is received, then nothing
    // is streamed to the output file and the error response is returned by FullRequestNoBody.
    const expected_status = 200;
    rest.setResponseBodyStream(expected_status, true, file_stream) catch {};

    const response_str = 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", .{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;
    }

    // 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": "hamlet.xml",
    //   "path_lower": "/homework/lit/hamlet.xml",
    //   "path_display": "/Homework/lit/hamlet.xml",
    //   "id": "id:74FkdeNuyKAAAAAAAAAAAQ",
    //   "client_modified": "2016-06-02T23:19:00Z",
    //   "server_modified": "2016-06-02T23:19:00Z",
    //   "rev": "9482db15f",
    //   "size": 279658
    // }

    // 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() });
}