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

Constant Contact V3: Create a contact

See more Constant Contact Examples

Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat Rust Downloads

Rust
let mut success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.
let vault = chilkat::Secrets::new();
vault.set_location("local_manager");
vault.set_auto_split(true);
let entry = chilkat::JsonObject::new();
let _ = entry.update_string("appName", "ConstantContactV3");
let _ = entry.update_string("service", "OAuth2");
let _ = entry.update_string("username", "my-account");
let tokens = chilkat::JsonObject::new();
if vault.get_secret_json(&entry, &tokens).is_err() {
    println!("Run the desktop authorization example using this account and store.");
    return;
}

if tokens.json_type_of("access_token") != 1 {
    println!("The saved authorization has no access token.");
    return;
}

let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&tokens.string_of("access_token").unwrap_or_default());
let resp = chilkat::HttpResponse::new();

let body = chilkat::JsonObject::new();
let _ = body.update_string("create_source", "Account");
let _ = body.update_string("first_name", "Alex");
let _ = body.update_string("last_name", "Rivera");
let _ = body.update_string("email_address.address", "reader+newsletter@example.com");
// This sample assumes the account has recorded a qualifying existing relationship.
let _ = body.update_string("email_address.permission_to_send", "implicit");
let _ = body.update_string("street_addresses[0].kind", "work");
let _ = body.update_string("street_addresses[0].street", "100 Main Street");
let _ = body.update_string("street_addresses[0].city", "Boston");
let _ = body.update_string("street_addresses[0].state", "MA");
let _ = body.update_string("street_addresses[0].country", "United States");
success = http.http_json("POST", "https://api.cc.email/v3/contacts", &body, "application/json", &resp).is_ok();
if resp.status_code() == 409 {
    println!("Contact already exists. Look up the email address to obtain its ID.");
    return;
}

if (!success) || (resp.status_code() != 201) {
    println!("Expected HTTP 201. Received {}", resp.status_code());
    if resp.status_code() == 401 {
        println!("Refresh the saved access token or authorize again.");
    }

    return;
}

let created = chilkat::JsonObject::new();
if created.load(&resp.body_str()).is_err() {
    println!("Expected a JSON response.");
    return;
}

if (created.json_type_of("contact_id") != 1) || (created.string_of_equals("contact_id", "", true)) {
    println!("Missing contact_id in the response.");
    return;
}

println!("Created contact ID: {}", created.string_of("contact_id").unwrap_or_default());