Sample code for 30+ languages & platforms
Rust

GetHarvest - Create Contact

See more GetHarvest Examples

Creates a new contact object. Returns a contact object and a 201 Created response code if the call succeeded.

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 "https://api.harvestapp.com/v2/contacts" \
//   -H "Authorization: Bearer ACCESS_TOKEN" \
//   -H "Harvest-Account-Id: ACCOUNT_ID" \
//   -H "User-Agent: MyApp (yourname@example.com)" \
//   -X POST \
//   -H "Content-Type: application/json" \
//   -d '{"client_id":8282839,"first_name":"George","last_name":"Frank","email":"georgefrank@example.com"}'

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

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

// {
//   "client_id": 8282839,
//   "first_name": "George",
//   "last_name": "Frank",
//   "email": "georgefrank@example.com"
// }

let json = chilkat::JsonObject::new();
let _ = json.update_int("client_id", 8282839);
let _ = json.update_string("first_name", "George");
let _ = json.update_string("last_name", "Frank");
let _ = json.update_string("email", "georgefrank@example.com");

http.set_request_header("User-Agent", "MyApp (yourname@example.com)");
http.set_request_header("Content-Type", "application/json");
http.set_request_header("Authorization", "Bearer ACCESS_TOKEN");
http.set_request_header("Harvest-Account-Id", "ACCOUNT_ID");

let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://api.harvestapp.com/v2/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:

// {
//   "id": 4706510,
//   "title": null,
//   "first_name": "George",
//   "last_name": "Frank",
//   "email": "georgefrank@example.com",
//   "phone_office": "",
//   "phone_mobile": "",
//   "fax": "",
//   "created_at": "2017-06-26T21:44:57Z",
//   "updated_at": "2017-06-26T21:44:57Z",
//   "client": {
//     "id": 5735776,
//     "name": "123 Industries"
//   }
// }

// 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 id = j_resp.int_of("id");
let title = j_resp.string_of("title").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 email = j_resp.string_of("email").unwrap_or_default();
let phone_office = j_resp.string_of("phone_office").unwrap_or_default();
let phone_mobile = j_resp.string_of("phone_mobile").unwrap_or_default();
let fax = j_resp.string_of("fax").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();
let client_id = j_resp.int_of("client.id");
let client_name = j_resp.string_of("client.name").unwrap_or_default();