Sample code for 30+ languages & platforms
Rust

Debug REST HTTP Request

See more REST Examples

Demonstrates how to generate the HTTP Request (with all headers intact) without actually sending the request.

Note: This example requires Chilkat v9.5.0.77 or later.

Chilkat Rust Downloads

Rust

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

// This example will connect to the web server, but does not actually send a request.
// When in DebugMode, the request is composed in memory and can be retrieved by calling
// GetLastDebugRequest.

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

// Connect Code...
// URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
if rest.connect("test-api.service.hmrc.gov.uk", port, b_tls, b_auto_reconnect).is_err() {
    println!("ConnectFailReason: {}", rest.connect_fail_reason());
    println!("{}", rest.last_error_text());
    return;
}

// Build the request body...
let json = chilkat::JsonObject::new();
let _ = json.update_string("periodKey", "A001");
let _ = json.update_number("vatDueSales", "105.50");
let _ = json.update_number("vatDueAcquisitions", "-100.45");
let _ = json.update_number("totalVatDue", "5.05");
let _ = json.update_number("vatReclaimedCurrPeriod", "105.15");
let _ = json.update_number("netVatDue", "100.10");
let _ = json.update_int("totalValueSalesExVAT", 300);
let _ = json.update_int("totalValuePurchasesExVAT", 300);
let _ = json.update_int("totalValueGoodsSuppliedExVAT", 3000);
let _ = json.update_int("totalAcquisitionsExVAT", 3000);
let _ = json.update_bool("finalised", true);

// Add Headers...
let _ = rest.add_header("Accept", "application/vnd.hmrc.1.0+json");
let _ = rest.add_header("Authorization", "Bearer HMRC_ACCESS_TOKEN");
let _ = rest.add_header("Content-Type", "application/json");

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

// Set DebugMode so that no request is actually sent.
rest.set_debug_mode(true);

let sb_response_body = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/organisations/vat/MY_HMRC_VRN/returns", &sb_request_body, &sb_response_body).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// Get the exact contents of what would've been sent.
// This includes the HTTP start line, the HTTP request headers, and the request body.
// Given that it's possible for the request body to contain binary data,
// the GetLastDebugRequest fetches into a BinData object.
// In this case, however, our request body contained JSON, so we can
// examine it as a string..
let bd_request = chilkat::BinData::new();
let _ = rest.get_last_debug_request(&bd_request).is_ok();

println!("----");
println!("{}", bd_request.get_string("utf-8").unwrap_or_default());
println!("----");

// The output for the above case:

// POST /organisations/vat/MY_HMRC_VRN/returns HTTP/1.1
// Accept: application/vnd.hmrc.1.0+json
// Host: test-api.service.hmrc.gov.uk
// Authorization: Bearer HMRC_ACCESS_TOKEN
// Content-Type: application/json
// Content-Length: 281
// 
// {"periodKey":"A001","vatDueSales":105.50, ... ,"finalised":true}
// 
//