Sample code for 30+ languages & platforms
Rust

IBM Cloud - Text to Speech - Synthesize Audio (POST)

See more IBM Text to Speech Examples

Synthesizes text to audio that is spoken in the specified voice. Uses a POST which allows for more text to be synthesized.

Chilkat Rust Downloads

Rust

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

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

http.set_login("apikey");
http.set_password("my_apikey");
http.set_basic_auth(true);

// Send the following request:

// curl -X POST -u "apikey:{apikey}" 
//    --header "Content-Type: application/json" 
//    --header "Accept: audio/wav" 
//    --data "{\"text\":\"When life gives you lemons, order the lobster tail.\"}" 
//    --output lobster.wav "{url}/v1/synthesize?voice=en-US_AllisonV3Voice"

// Use your base URL shown in the credentials web page (below your apikey)
let my_base_url = "https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/31941e96-7b89-4d56-8993-9cd8f18ec2d8".to_string();

let sb_url = chilkat::StringBuilder::new();
let _ = sb_url.append(&my_base_url);
let _ = sb_url.append("/v1/synthesize?voice=en-US_AllisonV3Voice");

http.set_accept("audio/wav");

let json = chilkat::JsonObject::new();
let _ = json.update_string("text", "When life gives you lemons, order the lobster tail.");

let url = sb_url.get_as_string().unwrap_or_default();
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", &url, &json, "application/json", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let resp_status_code = resp.status_code();
println!("response status code = {}", resp_status_code);

if resp_status_code == 200 {
    let _ = resp.save_body_binary("qa_output/lobster.wav").is_ok();
    println!("Success!");
} else {
    println!("{}", resp.body_str());
}