Rust
Rust
Quickbooks Create a New Customer
See more QuickBooks Examples
Demonstrates how to create a new customer via the Quickbooks REST API.Chilkat Rust Downloads
// 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:
// {
// "FullyQualifiedName": "King Groceries",
// "PrimaryEmailAddr": {
// "Address": "jdrew@myemail.com"
// },
// "DisplayName": "King's Groceries",
// "Suffix": "Jr",
// "Title": "Mr",
// "MiddleName": "B",
// "Notes": "Here are other details.",
// "FamilyName": "King",
// "PrimaryPhone": {
// "FreeFormNumber": "(555) 555-5555"
// },
// "CompanyName": "King Groceries",
// "BillAddr": {
// "CountrySubDivisionCode": "CA",
// "City": "Mountain View",
// "PostalCode": "94042",
// "Line1": "123 Main Street",
// "Country": "USA"
// },
// "GivenName": "James"
// }
//
// 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("FullyQualifiedName", "King Groceries");
let _ = json_req.update_string("PrimaryEmailAddr.Address", "jdrew@myemail.com");
let _ = json_req.update_string("DisplayName", "King's Groceries");
let _ = json_req.update_string("Suffix", "Jr");
let _ = json_req.update_string("Title", "Mr");
let _ = json_req.update_string("MiddleName", "B");
let _ = json_req.update_string("Notes", "Here are other details.");
let _ = json_req.update_string("FamilyName", "King");
let _ = json_req.update_string("PrimaryPhone.FreeFormNumber", "(555) 555-5555");
let _ = json_req.update_string("CompanyName", "King Groceries");
let _ = json_req.update_string("BillAddr.CountrySubDivisionCode", "CA");
let _ = json_req.update_string("BillAddr.City", "Mountain View");
let _ = json_req.update_string("BillAddr.PostalCode", "94042");
let _ = json_req.update_string("BillAddr.Line1", "123 Main Street");
let _ = json_req.update_string("BillAddr.Country", "USA");
let _ = json_req.update_string("GivenName", "James");
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>/customer", &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
// {
// "Customer": {
// "domain": "QBO",
// "PrimaryEmailAddr": {
// "Address": "jdrew@myemail.com"
// },
// "DisplayName": "King's Groceries",
// "CurrencyRef": {
// "name": "United States Dollar",
// "value": "USD"
// },
// "DefaultTaxCodeRef": {
// "value": "2"
// },
// "PreferredDeliveryMethod": "Print",
// "GivenName": "James",
// "FullyQualifiedName": "King's Groceries",
// "BillWithParent": false,
// "Title": "Mr",
// "Job": false,
// "BalanceWithJobs": 0,
// "PrimaryPhone": {
// "FreeFormNumber": "(555) 555-5555"
// },
// "Taxable": true,
// "MetaData": {
// "CreateTime": "2015-07-23T10:58:12-07:00",
// "LastUpdatedTime": "2015-07-23T10:58:12-07:00"
// },
// "BillAddr": {
// "City": "Mountain View",
// "Country": "USA",
// "Line1": "123 Main Street",
// "PostalCode": "94042",
// "CountrySubDivisionCode": "CA",
// "Id": "112"
// },
// "MiddleName": "B",
// "Notes": "Here are other details.",
// "Active": true,
// "Balance": 0,
// "SyncToken": "0",
// "Suffix": "Jr",
// "CompanyName": "King Groceries",
// "FamilyName": "King",
// "PrintOnCheckName": "King Groceries",
// "sparse": false,
// "Id": "67"
// },
// "time": "2015-07-23T10:58:12.099-07:00"
// }
//
let customer_domain = json_response.string_of("Customer.domain").unwrap_or_default();
let customer_primary_email_addr_address = json_response.string_of("Customer.PrimaryEmailAddr.Address").unwrap_or_default();
let customer_display_name = json_response.string_of("Customer.DisplayName").unwrap_or_default();
let customer_currency_ref_name = json_response.string_of("Customer.CurrencyRef.name").unwrap_or_default();
let customer_currency_ref_value = json_response.string_of("Customer.CurrencyRef.value").unwrap_or_default();
let customer_default_tax_code_ref_value = json_response.string_of("Customer.DefaultTaxCodeRef.value").unwrap_or_default();
let customer_preferred_delivery_method = json_response.string_of("Customer.PreferredDeliveryMethod").unwrap_or_default();
let customer_given_name = json_response.string_of("Customer.GivenName").unwrap_or_default();
let customer_fully_qualified_name = json_response.string_of("Customer.FullyQualifiedName").unwrap_or_default();
let customer_bill_with_parent = json_response.bool_of("Customer.BillWithParent");
let customer_title = json_response.string_of("Customer.Title").unwrap_or_default();
let customer_job = json_response.bool_of("Customer.Job");
let customer_balance_with_jobs = json_response.int_of("Customer.BalanceWithJobs");
let customer_primary_phone_free_form_number = json_response.string_of("Customer.PrimaryPhone.FreeFormNumber").unwrap_or_default();
let customer_taxable = json_response.bool_of("Customer.Taxable");
let customer_meta_data_create_time = json_response.string_of("Customer.MetaData.CreateTime").unwrap_or_default();
let customer_meta_data_last_updated_time = json_response.string_of("Customer.MetaData.LastUpdatedTime").unwrap_or_default();
let customer_bill_addr_city = json_response.string_of("Customer.BillAddr.City").unwrap_or_default();
let customer_bill_addr_country = json_response.string_of("Customer.BillAddr.Country").unwrap_or_default();
let customer_bill_addr_line1 = json_response.string_of("Customer.BillAddr.Line1").unwrap_or_default();
let customer_bill_addr_postal_code = json_response.string_of("Customer.BillAddr.PostalCode").unwrap_or_default();
let customer_bill_addr_country_sub_division_code = json_response.string_of("Customer.BillAddr.CountrySubDivisionCode").unwrap_or_default();
let customer_bill_addr_id = json_response.string_of("Customer.BillAddr.Id").unwrap_or_default();
let customer_middle_name = json_response.string_of("Customer.MiddleName").unwrap_or_default();
let customer_notes = json_response.string_of("Customer.Notes").unwrap_or_default();
let customer_active = json_response.bool_of("Customer.Active");
let customer_balance = json_response.int_of("Customer.Balance");
let customer_sync_token = json_response.string_of("Customer.SyncToken").unwrap_or_default();
let customer_suffix = json_response.string_of("Customer.Suffix").unwrap_or_default();
let customer_company_name = json_response.string_of("Customer.CompanyName").unwrap_or_default();
let customer_family_name = json_response.string_of("Customer.FamilyName").unwrap_or_default();
let customer_print_on_check_name = json_response.string_of("Customer.PrintOnCheckName").unwrap_or_default();
let customer_sparse = json_response.bool_of("Customer.sparse");
let customer_id = json_response.string_of("Customer.Id").unwrap_or_default();
let time = json_response.string_of("time").unwrap_or_default();