Rust
Rust
SMSAPI - Create a Contact
See more SMSAPI Examples
Create a ContactChilkat Rust Downloads
// 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 -X POST https://api.smsapi.com/contacts -H "Authorization: Bearer token_api_oauth" \
// -d "phone_number=48500000000&email=bok@smsapi.com&first_name=Name&last_name=Last_name&gender=gender&description=description&city=city&groups=default"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/contacts");
req.set_content_type("application/x-www-form-urlencoded");
req.add_param("phone_number", "48500000000");
req.add_param("email", "bok@smsapi.com");
req.add_param("first_name", "Name");
req.add_param("last_name", "Last_name");
req.add_param("gender", "gender");
req.add_param("description", "description");
req.add_param("city", "city");
req.add_param("groups", "default");
req.add_header("Authorization", "Bearer token_api_oauth");
let resp = chilkat::HttpResponse::new();
if http.http_req("https://api.smsapi.com/contacts", &req, &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)
// {
// "id": "5b802315a788494a04690d1d",
// "first_name": "string",
// "last_name": "string",
// "phone_number": "48500000000",
// "email": "bok@smsapi.com",
// "gender": "gender",
// "city": "city",
// "date_created": "2018-08-24T17:24:05+02:00",
// "date_updated": "2018-08-24T17:24:05+02:00",
// "description": "description",
// "groups": [
// {
// "id": "59a3ca1fa78849062837cd0c",
// "name": "default",
// "date_created": "2017-08-28T09:45:35+02:00",
// "date_updated": "2017-08-28T09:45:35+02:00",
// "description": "",
// "created_by": "username",
// "idx": null,
// "contact_expire_after": null,
// "contacts_count": 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 date_created = chilkat::DtObj::new();
let date_updated = chilkat::DtObj::new();
let mut id = j_resp.string_of("id").unwrap_or_default();
let first_name = j_resp.string_of("first_name").unwrap_or_default();
let last_name = j_resp.string_of("last_name").unwrap_or_default();
let phone_number = j_resp.string_of("phone_number").unwrap_or_default();
let email = j_resp.string_of("email").unwrap_or_default();
let gender = j_resp.string_of("gender").unwrap_or_default();
let city = j_resp.string_of("city").unwrap_or_default();
let _ = j_resp.dt_of("date_created", false, &date_created);
let _ = j_resp.dt_of("date_updated", false, &date_updated);
let mut description = j_resp.string_of("description").unwrap_or_default();
let mut i = 0;
let count_i = j_resp.size_of_array("groups");
while i < count_i {
j_resp.set_i(i);
id = j_resp.string_of("groups[i].id").unwrap_or_default();
let _ = j_resp.string_of("groups[i].name").unwrap_or_default();
let _ = j_resp.dt_of("groups[i].date_created", false, &date_created);
let _ = j_resp.dt_of("groups[i].date_updated", false, &date_updated);
description = j_resp.string_of("groups[i].description").unwrap_or_default();
let _ = j_resp.string_of("groups[i].created_by").unwrap_or_default();
let _ = j_resp.string_of("groups[i].idx").unwrap_or_default();
let _ = j_resp.string_of("groups[i].contact_expire_after").unwrap_or_default();
let _ = j_resp.string_of("groups[i].contacts_count").unwrap_or_default();
i = i + 1;
}