Sample code for 30+ languages & platforms
Rust

REST POST JSON using Gzip Content Encoding

See more REST Examples

Demonstrates how to send a JSON POST using the gzip Content-Encoding.

Chilkat Rust Downloads

Rust

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Use the online tool at https://tools.chilkat.io/Default.cshtml
// to generate the JSON code that creates this JSON:

// {
//     "lists": [
//         {
//             "id": "1511199999"
//         }
//     ],
//     "confirmed": false,
//     "email_addresses": [
//         {
//             "email_address": "support@chilkatsoft.com"
//         }
//     ],
//     "first_name": "Matt",
//     "last_name": "Smith"
// }
// 

let json = chilkat::JsonObject::new();
let _ = json.update_string("lists[0].id", "1511199999");
let _ = json.update_bool("confirmed", false);
let _ = json.update_string("email_addresses[0].email_address", "support@chilkatsoft.com");
let _ = json.update_string("first_name", "Matt");
let _ = json.update_string("last_name", "Smith");

let rest = chilkat::Rest::new();

if rest.connect("api.constantcontact.com", 443, true, true).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let _ = rest.add_header("Content-Type", "application/json");
let _ = rest.add_header("Authorization", "Bearer xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx");

// Tell the server you'll accept only an application/json response.
let _ = rest.add_header("Accept", "application/json");

// Indicate that we'll be sending the request body gzipped.
let _ = rest.add_header("Content-Encoding", "gzip");

let sb_req = chilkat::StringBuilder::new();
let _ = json.emit_sb(&sb_req);

// Send the POST.
let sb_resp = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=xxxxxxx", &sb_req, &sb_resp).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

println!("Response body:");
println!("{}", sb_resp.get_as_string().unwrap_or_default());

if rest.response_status_code() != 200 {
    println!("Received error response code: {}", rest.response_status_code());
    return;
}

let json_resp = chilkat::JsonObject::new();
let _ = json_resp.load_sb(&sb_resp);

// Use the online tool at https://tools.chilkat.io/jsonParse.cshtml
// to generate the JSON code that parses the JSON response..