Rust Requires Chilkat v11.4.0+
Rust
Finnhub API - Get Stock Quote
See more AI Examples
Demonstrates how to get a stock quote from the Finnhub API.Chilkat Rust Downloads
// Replace with your actual Finnhub API key.
let api_key = "YOUR_FINNHUB_API_KEY".to_string();
let symbol = "AAPL".to_string();
let http = chilkat::Http::new();
// This is the URL without params.
let url_without_params = "https://finnhub.io/api/v1/quote".to_string();
let req = chilkat::HttpRequest::new();
// Add params that will be sent in the URL.
req.add_param("symbol", &symbol);
req.add_param("token", &api_key);
req.set_http_verb("GET");
// Send the request to get the JSON response.
let resp = chilkat::HttpResponse::new();
if http.http_req(&url_without_params, &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let json = chilkat::JsonObject::new();
let _ = resp.get_body_json(&json);
let status_code = resp.status_code();
println!("response status code: {}", status_code);
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
// Sample result:
// {
// "c": 248.8,
// "d": -4.09,
// "dp": -1.6173,
// "h": 255.493,
// "l": 248.07,
// "o": 253.9,
// "pc": 252.89,
// "t": 1774641600
// }
if status_code == 200 {
// Add the symbol to the top of the result.
let _ = json.add_string_at(0, "symbol", &symbol);
// Rename members for clarification.
let _ = json.rename("c", "currentPrice");
let _ = json.rename("d", "change");
let _ = json.rename("dp", "percentChange");
let _ = json.rename("h", "high");
let _ = json.rename("l", "low");
let _ = json.rename("o", "open");
let _ = json.rename("pc", "prevClose");
let _ = json.rename("t", "unixTime");
println!("{}", json.emit().unwrap_or_default());
} else {
println!("Failed");
}