Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

VoiceBase -- Upload a media file for transcription and analysis

See more VoiceBase Examples

This example demonstrates how to upload a media file for transcription and analysis. It duplicates the following curl command:
curl https://apis.voicebase.com/v2-beta/media \
  --form media=@msg_123_abc.wav \
  --header "Authorization: Bearer ${TOKEN}"

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

    // Insert your Bearer token here:
    const access_token = "VOICEBASE_TOKEN";

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

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("POST");
    req.setPath("/v2-beta/media");
    req.setContentType("multipart/form-data");

    // Add the access (bearer) token to the request, which is a header
    // having the following format:
    // Authorization: Bearer <userAccessToken>
    const sb_auth = try chilkat.StringBuilder.init();
    defer sb_auth.deinit();
    sb_auth.append("Bearer ") catch {};
    sb_auth.append(access_token) catch {};
    req.addHeader("Authorization", try sb_auth.getAsString(alloc));

    req.addFileForUpload2("media", "qa_data/wav/msg_123_abc.wav", "audio/x-wav") catch {
        std.debug.print("{s}\n", .{try req.getLastErrorText(alloc)});
        return;
    };

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSReq("apis.voicebase.com", 443, true, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Examine the response status code and body.
    std.debug.print("Response status code = {d}\n", .{resp.getStatusCode()});

    // The response should be JSON, even if an error.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(try resp.getBodyStr(alloc)) catch {};
    json.setEmitCompact(false);

    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // A successful response will have a status code = 200
    if (resp.getStatusCode() != 200) {
        std.debug.print("Failed.\n", .{});
    } else {
        std.debug.print("mediaId: {s}\n", .{try json.stringOf(alloc, "mediaId")});
        std.debug.print("href: {s}\n", .{try json.stringOf(alloc, "_links.self.href")});
        std.debug.print("status: {s}\n", .{try json.stringOf(alloc, "status")});
        std.debug.print("Success.\n", .{});
    }

    // Here is an example of a successful response:

    //     {
    //       "_links": {
    //         "self": {
    //           "href": "/v2-beta/media/856a1e85-c847-4c3c-b7a4-6cf15cd51db4"
    //         }
    //       },
    //       "mediaId": "856a1e85-c847-4c3c-b7a4-6cf15cd51db4",
    //       "status": "accepted",
    //       "metadata": {}
    //     }
}