Zig Requires Chilkat v11.0.0+
Zig
VoiceBase -- Upload a Media File with a JSON Configuration
See more VoiceBase Examples
This example uploads a media file and also provides a configuration file.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 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;
};
// This is the JSON of the configuration to be added as a parameter to the upload:
// (Obviously, if you want a callback, you would use a URL that goes to your own web server..)
// {
// "configuration":{
// "publish":{
// "callbacks":[
// {
// "url":"https://www.chilkatsoft.com/dss/update_vm_transcription.asp",
// "method":"POST",
// "include":[
// "transcripts",
// "keywords",
// "topics",
// "metadata"
// ]
// }
// ]
// },
// "keywords":{
// "groups":[]
// }
// }
// }
// This code produces the above JSON.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("configuration.publish.callbacks[0].url", "https://www.chilkatsoft.com/dss/update_vm_transcription.asp") catch {};
json.updateString("configuration.publish.callbacks[0].method", "POST") catch {};
json.updateString("configuration.publish.callbacks[0].include[0]", "transcripts") catch {};
json.updateString("configuration.publish.callbacks[0].include[1]", "keywords") catch {};
json.updateString("configuration.publish.callbacks[0].include[2]", "topics") catch {};
json.updateString("configuration.publish.callbacks[0].include[3]", "metadata") catch {};
const j_config = try chilkat.JsonObject.init();
defer j_config.deinit();
json.objectOf2("configuration", j_config) catch {};
const j_keywords = try chilkat.JsonObject.init();
defer j_keywords.deinit();
j_config.appendObject2("keywords", j_keywords) catch {};
const j_groups = try chilkat.JsonArray.init();
defer j_groups.deinit();
j_keywords.appendArray2("groups", j_groups) catch {};
// Add the configuration JSON to the upload.
req.addParam("configuration", try json.emit(alloc));
// Do the upload..
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.
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": {}
// }
}