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

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 Rust Downloads

Rust

// This example assumes the Chilkat HTTP API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Insert your Bearer token here:
let access_token = "VOICEBASE_TOKEN".to_string();

let http = chilkat::Http::new();

let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/v2-beta/media");
req.set_content_type("multipart/form-data");

// Add the access (bearer) token to the request, which is a header
// having the following format:
// Authorization: Bearer <userAccessToken>
let sb_auth = chilkat::StringBuilder::new();
let _ = sb_auth.append("Bearer ");
let _ = sb_auth.append(&access_token);
req.add_header("Authorization", &sb_auth.get_as_string().unwrap_or_default());

if req.add_file_for_upload2("media", "qa_data/wav/msg_123_abc.wav", "audio/x-wav").is_err() {
    println!("{}", req.last_error_text());
    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.
let json = chilkat::JsonObject::new();
let _ = json.update_string("configuration.publish.callbacks[0].url", "https://www.chilkatsoft.com/dss/update_vm_transcription.asp");
let _ = json.update_string("configuration.publish.callbacks[0].method", "POST");
let _ = json.update_string("configuration.publish.callbacks[0].include[0]", "transcripts");
let _ = json.update_string("configuration.publish.callbacks[0].include[1]", "keywords");
let _ = json.update_string("configuration.publish.callbacks[0].include[2]", "topics");
let _ = json.update_string("configuration.publish.callbacks[0].include[3]", "metadata");

let j_config = chilkat::JsonObject::new();
let _ = json.object_of2("configuration", &j_config);

let j_keywords = chilkat::JsonObject::new();
let _ = j_config.append_object2("keywords", &j_keywords);

let j_groups = chilkat::JsonArray::new();
let _ = j_keywords.append_array2("groups", &j_groups);

// Add the configuration JSON to the upload.
req.add_param("configuration", &json.emit().unwrap_or_default());

// Do the upload..
let resp = chilkat::HttpResponse::new();
if http.http_s_req("apis.voicebase.com", 443, true, &req, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

// Examine the response status code and body.
println!("Response status code = {}", resp.status_code());

// The response should be JSON, even if an error.
let _ = json.load(&resp.body_str());
json.set_emit_compact(false);

println!("{}", json.emit().unwrap_or_default());

// A successful response will have a status code = 200
if resp.status_code() != 200 {
    println!("Failed.");
} else {
    println!("mediaId: {}", json.string_of("mediaId").unwrap_or_default());
    println!("href: {}", json.string_of("_links.self.href").unwrap_or_default());
    println!("status: {}", json.string_of("status").unwrap_or_default());
    println!("Success.");
}

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