Sample code for 30+ languages & platforms
Zig

Dropbox File Streaming Upload

See more Dropbox Examples

Upload a file to Drobox using a file stream. This example can upload files up to 150MB. Larger files must be uploaded with an upload session (upload_session/start).

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("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": "/Homework/lit/hamlet.xml",
    //    "mode": "add",
    //    "autorename": true,
    //    "mute": false
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.appendString("path", "/Homework/lit/hamlet.xml") 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 {};

    // Almost ready to go...
    // Let's setup a file stream to point to a file.
    const file_stream = try chilkat.Stream.init();
    defer file_stream.deinit();
    file_stream.setSourceFile("qa_data/xml/hamlet.xml");

    // 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".

    // Note: The file is streamed directly from disk.  (The entire
    // file will not be loaded into memory.)
    const response_str = rest.fullRequestStream(alloc, "POST", "/2/files/upload", file_stream) 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;
    }

    // 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": "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
    // }

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