Sample code for 30+ languages & platforms
Rust

Clickatell Send SMS Text Message

See more Clickatell Examples

Demonstrate how to send a Clickatell SMS text message.

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();

// Implements the following CURL command:

// curl -i \
// -X POST \
// -H "Content-Type: application/json" \
// -H "Accept: application/json" \
// -H "Authorization: API_KEY" \
// -d '{"content": "Test Message Text", "to": ["PHONE_NUMBER"], "from": "FROM_PHONE_NUMBER" }' \
// -s https://platform.clickatell.com/messages

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

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

// {
//   "content": "Test Message Text",
//   "to": [
//     "PHONE_NUMBER"
//   ]
// }

let json = chilkat::JsonObject::new();
let _ = json.update_string("content", "Test Message Text");
let _ = json.update_string("to[0]", "PHONE_NUMBER");
let _ = json.update_string("from", "FROM_PHONE_NUMBER");

http.set_request_header("Authorization", "API_KEY");
http.set_request_header("Accept", "application/json");
http.set_request_header("Content-Type", "application/json");

// If the following URL does not work, then try "https://platform.clickatell.com/v1/message"
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://platform.clickatell.com/messages", &json, "application/json", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);

println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());

let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
    println!("Response Header:");
    println!("{}", resp.header());
    println!("Failed.");
    return;
}

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

// {
//   "messages": [
//     {
//       "apiMessageId": "66c06ac68e5c4afb953ea9328babfde6",
//       "accepted": true,
//       "to": "16302581871",
//       "errorCode": null,
//       "error": null,
//       "errorDescription": null
//     }
//   ],
//   "errorCode": null,
//   "error": null,
//   "errorDescription": null
// }

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

let mut i: i32 = 0;

let _ = j_resp.string_of("errorCode").unwrap_or_default();
let _ = j_resp.string_of("error").unwrap_or_default();
let _ = j_resp.string_of("errorDescription").unwrap_or_default();
i = 0;
let count_i = j_resp.size_of_array("messages");
while i < count_i {
    j_resp.set_i(i);
    let _ = j_resp.string_of("messages[i].apiMessageId").unwrap_or_default();
    let _ = j_resp.bool_of("messages[i].accepted");
    let _ = j_resp.string_of("messages[i].to").unwrap_or_default();
    let _ = j_resp.string_of("messages[i].errorCode").unwrap_or_default();
    let _ = j_resp.string_of("messages[i].error").unwrap_or_default();
    let _ = j_resp.string_of("messages[i].errorDescription").unwrap_or_default();
    i = i + 1;
}