Sample code for 30+ languages & platforms
Rust

Constant Contact: Create a Contact

See more Constant Contact Examples

Creates a single contact.

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 -X POST \
//   https://api.cc.email/v3/contacts \
//   -H 'accept: application/json' \
//   -H 'authorization: Bearer {access_token}' \
//   -H 'cache-control: no-cache' \
//   -H 'content-type: application/json' \
//   -d '{
//   "email_address": {
//     "address": "danipper@example.com",
//     "permission_to_send": "implicit"
//   },
//   "first_name": "David",
//   "last_name": "Nipper",
//   "job_title": "Musician",
//   "company_name": "Acme Corp.",
//   "create_source": "Account",
//   "birthday_month": 11,
//   "birthday_day": 24,
//   "anniversary": "2006-11-15",
//   "steet_addresses":{
//     "kind":"home",
//     "street": "123 Kashmir Valley Road",
//     "city": "Chicago",
//     "state": "Illinois",
//     "country": "United States",
//   }
// }'

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

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

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

// {
//   "email_address": {
//     "address": "danipper@example.com",
//     "permission_to_send": "implicit"
//   },
//   "first_name": "David",
//   "last_name": "Nipper",
//   "job_title": "Musician",
//   "company_name": "Acme Corp.",
//   "create_source": "Account",
//   "birthday_month": 11,
//   "birthday_day": 24,
//   "anniversary": "2006-11-15",
//   "steet_addresses": {
//     "kind": "home",
//     "street": "123 Kashmir Valley Road",
//     "city": "Chicago",
//     "state": "Illinois",
//     "country": "United States"
//   }
// }

let json = chilkat::JsonObject::new();
let _ = json.update_string("email_address.address", "danipper@example.com");
let _ = json.update_string("email_address.permission_to_send", "implicit");
let _ = json.update_string("first_name", "David");
let _ = json.update_string("last_name", "Nipper");
let _ = json.update_string("job_title", "Musician");
let _ = json.update_string("company_name", "Acme Corp.");
let _ = json.update_string("create_source", "Account");
let _ = json.update_int("birthday_month", 11);
let _ = json.update_int("birthday_day", 24);
let _ = json.update_string("anniversary", "2006-11-15");
let _ = json.update_string("steet_addresses.kind", "home");
let _ = json.update_string("steet_addresses.street", "123 Kashmir Valley Road");
let _ = json.update_string("steet_addresses.city", "Chicago");
let _ = json.update_string("steet_addresses.state", "Illinois");
let _ = json.update_string("steet_addresses.country", "United States");

http.set_request_header("content-type", "application/json");
// Adds the "Authorization: Bearer ACCESS_TOKEN" header.
http.set_auth_token("ACCESS_TOKEN");
http.set_request_header("cache-control", "no-cache");
http.set_request_header("accept", "application/json");

let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://api.cc.email/v3/contacts", &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)

// {
//   "contact_id": "{contact_id}",
//   "email_address": {
//     "address": "danipper@example.com",
//     "permission_to_send": "implicit",
//     "created_at": "2018-02-23T13:38:28-05:00",
//     "updated_at": "2018-02-23T13:38:28-05:00",
//     "opt_in_source": "Account",
//     "opt_in_date": "2018-02-23T13:38:28-05:00",
//     "confirm_status": "off"
//   },
//   "first_name": "David",
//   "last_name": "Nipper",
//   "job_title": "Musician",
//   "company_name": "Acme Corp.",
//   "birthday_month": 11,
//   "birthday_day": 24,
//   "anniversary": "2006-11-15",
//   "create_source": "Account",
//   "created_at": "2018-02-23T13:38:28-05:00",
//   "updated_at": "2018-02-23T13:38:28-05:00"
// }

// 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 contact_id = j_resp.string_of("contact_id").unwrap_or_default();
let email_address_address = j_resp.string_of("email_address.address").unwrap_or_default();
let email_address_permission_to_send = j_resp.string_of("email_address.permission_to_send").unwrap_or_default();
let email_address_created_at = j_resp.string_of("email_address.created_at").unwrap_or_default();
let email_address_updated_at = j_resp.string_of("email_address.updated_at").unwrap_or_default();
let email_address_opt_in_source = j_resp.string_of("email_address.opt_in_source").unwrap_or_default();
let email_address_opt_in_date = j_resp.string_of("email_address.opt_in_date").unwrap_or_default();
let email_address_confirm_status = j_resp.string_of("email_address.confirm_status").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 job_title = j_resp.string_of("job_title").unwrap_or_default();
let company_name = j_resp.string_of("company_name").unwrap_or_default();
let birthday_month = j_resp.int_of("birthday_month");
let birthday_day = j_resp.int_of("birthday_day");
let anniversary = j_resp.string_of("anniversary").unwrap_or_default();
let create_source = j_resp.string_of("create_source").unwrap_or_default();
let created_at = j_resp.string_of("created_at").unwrap_or_default();
let updated_at = j_resp.string_of("updated_at").unwrap_or_default();