Sample code for 30+ languages & platforms
Rust

POST JSON Without Waiting for the Response

See more REST Examples

This example demonstrates sending a POST w/ content-type "application/json" where the body of the POST contains a JSON document. The POST is sent, but we don't wait for the response.

Chilkat Rust Downloads

Rust

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

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

// Connect to the server using TLS
let b_auto_reconnect = false;
if rest.connect("example.com", 443, true, b_auto_reconnect).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// Create some sample JSON to be sent in the body of the request.
let sb_json = chilkat::StringBuilder::new();
let _ = sb_json.append("{\"create\": [{\"name\": \"Woo Single #1\",\"type\": \"simple\",\"regular_price\": \"21.99\"}]}");

// Add the Content-Type HTTP request header.
let _ = rest.add_header("Content-Type", "application/json");

// Just send the request.  Don't read the response.
// The request is sent to https://example.com/something?arg1=xyz&arg2=abc
let some_path = "/something?arg1=xyz&arg2=abc".to_string();
if rest.send_req_sb("POST", &some_path, &sb_json).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// OK, the request was sent.
// Close the connection.
let max_wait_ms = 50;
let _ = rest.disconnect(max_wait_ms);

println!("JSON POST Sent.");