Sample code for 30+ languages & platforms
Rust

Quickbooks Delete an Invoice

See more QuickBooks Examples

Demonstrates how to delete an invoice using the Quickbooks REST API.

Chilkat Rust Downloads

Rust

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

// First get our previously obtained OAuth2 access token.
let json_token = chilkat::JsonObject::new();
let _ = json_token.load_file("qa_data/tokens/qb-access-token.json").is_ok();

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

// Connect to the REST server.
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("sandbox-quickbooks.api.intuit.com", port, b_tls, b_auto_reconnect).is_ok();

let sb_auth = chilkat::StringBuilder::new();
let _ = sb_auth.append("Bearer ");
let _ = sb_auth.append(&json_token.string_of("access_token").unwrap_or_default());
rest.set_authorization(&sb_auth.get_as_string().unwrap_or_default());

// --------------------------------------------------------------------------
// Note: The above code to setup the initial REST connection
// can be done once.  After connecting, any number of REST calls can be made.
// If the connection is lost, the next REST method call will automatically
// reconnect if needed.
// --------------------------------------------------------------------------

// Create the following JSON:

// {
//   "SyncToken": "3",
//   "Id": "33"
// }
// 
// Use the this online tool to generate the code from sample JSON: 
// Generate Code to Create JSON

let json_req = chilkat::JsonObject::new();
let _ = json_req.update_string("SyncToken", "3");
let _ = json_req.update_string("Id", "33");

let sb_request_body = chilkat::StringBuilder::new();
let _ = json_req.emit_sb(&sb_request_body);

let _ = rest.add_header("Content-Type", "application/json");
let _ = rest.add_header("Accept", "application/json");
rest.set_allow_header_folding(false);

let sb_response_body = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/v3/company/<realmID>/invoice?operation=delete", &sb_request_body, &sb_response_body).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let resp_status_code = rest.response_status_code();

// Success is indicated by a 200 response status code.
println!("response status code = {}", resp_status_code);

let json_response = chilkat::JsonObject::new();
let _ = json_response.load_sb(&sb_response_body);
json_response.set_emit_compact(false);
println!("{}", json_response.emit().unwrap_or_default());

if rest.response_status_code() != 200 {
    println!("Failed.");
    return;
}

// Sample output...
// (See the parsing code below..)
// 
// Use the this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

// {
//   "Invoice": {
//     "status": "Deleted",
//     "domain": "QBO",
//     "Id": "33"
//   },
//   "time": "2013-03-15T00:18:15.322-07:00"
// }
// 

let invoice_status = json_response.string_of("Invoice.status").unwrap_or_default();
let invoice_domain = json_response.string_of("Invoice.domain").unwrap_or_default();
let invoice_id = json_response.string_of("Invoice.Id").unwrap_or_default();
let time = json_response.string_of("time").unwrap_or_default();