Sample code for 30+ languages & platforms
Zig

Create a Temporary Public Link to a DropBox File

See more Dropbox Examples

Get a temporary link to stream content of a file. This link will expire in four hours and afterwards you will get 410 Gone.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Implements the following CURL command:

    // curl -X POST https://api.dropboxapi.com/2/files/get_temporary_link \
    //     --header "Authorization: Bearer DROPBOX_ACCESS_TOKEN" \
    //     --header "Content-Type: application/json" \
    //     --data "{\"path\": \"/video.mp4\"}"

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "path": "/video.mp4"
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("path", "/video.mp4") catch {};

    http.setRequestHeader("Authorization", "Bearer DROPBOX_ACCESS_TOKEN");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://api.dropboxapi.com/2/files/get_temporary_link", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "metadata": {
    //     "name": "Prime_Numbers.txt",
    //     "id": "id:a4ayc_80_OEAAAAAAAAAXw",
    //     "client_modified": "2015-05-12T15:50:38Z",
    //     "server_modified": "2015-05-12T15:50:38Z",
    //     "rev": "a1c10ce0dd78",
    //     "size": 7212,
    //     "path_lower": "/homework/math/prime_numbers.txt",
    //     "path_display": "/Homework/math/Prime_Numbers.txt",
    //     "sharing_info": {
    //       "read_only": true,
    //       "parent_shared_folder_id": "84528192421",
    //       "modified_by": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc"
    //     },
    //     "is_downloadable": true,
    //     "property_groups": [
    //       {
    //         "template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa",
    //         "fields": [
    //           {
    //             "name": "Security Policy",
    //             "value": "Confidential"
    //           }
    //         ]
    //       }
    //     ],
    //     "has_explicit_shared_members": false,
    //     "content_hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    //   },
    //   "link": "https://dl.dropboxusercontent.com/apitl/1/YXNkZmFzZGcyMzQyMzI0NjU2NDU2NDU2"
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var template_id: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var name: [:0]const u8 = "";
    var value: [:0]const u8 = "";

    _ = try j_resp.stringOf(alloc, "metadata.name");
    _ = try j_resp.stringOf(alloc, "metadata.id");
    _ = try j_resp.stringOf(alloc, "metadata.client_modified");
    _ = try j_resp.stringOf(alloc, "metadata.server_modified");
    _ = try j_resp.stringOf(alloc, "metadata.rev");
    _ = j_resp.intOf("metadata.size");
    _ = try j_resp.stringOf(alloc, "metadata.path_lower");
    _ = try j_resp.stringOf(alloc, "metadata.path_display");
    _ = j_resp.boolOf("metadata.sharing_info.read_only");
    _ = try j_resp.stringOf(alloc, "metadata.sharing_info.parent_shared_folder_id");
    _ = try j_resp.stringOf(alloc, "metadata.sharing_info.modified_by");
    _ = j_resp.boolOf("metadata.is_downloadable");
    _ = j_resp.boolOf("metadata.has_explicit_shared_members");
    _ = try j_resp.stringOf(alloc, "metadata.content_hash");
    _ = try j_resp.stringOf(alloc, "link");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("metadata.property_groups");
    while (i < count_i) {
        j_resp.setI(i);
        template_id = try j_resp.stringOf(alloc, "metadata.property_groups[i].template_id");
        j = 0;
        count_j = j_resp.sizeOfArray("metadata.property_groups[i].fields");
        while (j < count_j) {
            j_resp.setJ(j);
            name = try j_resp.stringOf(alloc, "metadata.property_groups[i].fields[j].name");
            value = try j_resp.stringOf(alloc, "metadata.property_groups[i].fields[j].value");
            j = j + 1;
        }

        i = i + 1;
    }
}