Unicode C++
Unicode C++
Finnhub API - Get Stock Quote
See more AI Examples
Demonstrates how to get a stock quote from the Finnhub API.Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
void ChilkatSample(void)
{
bool success = false;
// Replace with your actual Finnhub API key.
const wchar_t *apiKey = L"YOUR_FINNHUB_API_KEY";
const wchar_t *symbol = L"AAPL";
CkHttpW http;
// This is the URL without params.
const wchar_t *urlWithoutParams = L"https://finnhub.io/api/v1/quote";
CkHttpRequestW req;
// Add params that will be sent in the URL.
req.AddParam(L"symbol",symbol);
req.AddParam(L"token",apiKey);
req.put_HttpVerb(L"GET");
// Send the request to get the JSON response.
CkHttpResponseW resp;
success = http.HttpReq(urlWithoutParams,req,resp);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
CkJsonObjectW json;
resp.GetBodyJson(json);
int statusCode = resp.get_StatusCode();
wprintf(L"response status code: %d\n",statusCode);
json.put_EmitCompact(false);
wprintf(L"%s\n",json.emit());
// 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 (statusCode == 200) {
// Add the symbol to the top of the result.
json.AddStringAt(0,L"symbol",symbol);
// Rename members for clarification.
json.Rename(L"c",L"currentPrice");
json.Rename(L"d",L"change");
json.Rename(L"dp",L"percentChange");
json.Rename(L"h",L"high");
json.Rename(L"l",L"low");
json.Rename(L"o",L"open");
json.Rename(L"pc",L"prevClose");
json.Rename(L"t",L"unixTime");
wprintf(L"%s\n",json.emit());
}
else {
wprintf(L"Failed\n");
}
}