Sample code for 30+ languages & platforms
Rust

HTTP GET with Custom Header and OAuth2 Bearer Token

See more HTTP Examples

Demonstrate how to send a GET request with customer headers and an "Authorization: Bearer " header.

Chilkat Rust Downloads

Rust

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

let http = chilkat::Http::new();

// Setting the AuthToken property causes the "Authorization: Bearer <token>" header to be adeded.
http.set_auth_token("Just_the_access_token_here");

// Add one or more custom headers..
http.set_request_header("X-Tenant-ID", "value goes here");
http.set_request_header("blah-blah-blah", "value goes here");

let url = "https://www.example.com/abc/123?x=something&y=someOtherThing".to_string();

// Send the GET request and get the response body in the StringBuilder object.
let sb = chilkat::StringBuilder::new();
if http.quick_get_sb(&url, &sb).is_err() {
    println!("{}", http.last_error_text());
    return;
}

println!("response status code: {}", http.last_status());
println!("response body:");
println!("{}", sb.get_as_string().unwrap_or_default());

// If the response contains JSON, you can load it into a Chilkat JSON object...
let json = chilkat::JsonObject::new();
let _ = json.load_sb(&sb);
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());