Unicode C
Unicode C
Google Maps Geolocation Request
See more REST Examples
Demonstrates how make a Google Maps Geolocation REST API request.Chilkat Unicode C Downloads
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkJsonArrayW.h>
void ChilkatSample(void)
{
BOOL success;
HCkRestW rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkJsonObjectW json;
HCkJsonArrayW aCellTowers;
HCkJsonObjectW oCellTower;
HCkJsonArrayW aWifi;
HCkJsonObjectW oPoint;
const wchar_t *responseJson;
HCkJsonObjectW jsonResp;
HCkJsonObjectW jsonLoc;
const wchar_t *latitude;
const wchar_t *longitude;
const wchar_t *accuracy;
success = FALSE;
// This example duplicates the following CURL request:
// curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY"
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest = CkRestW_Create();
// Connect to the Google API REST server.
bTls = TRUE;
port = 443;
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"www.googleapis.com",port,bTls,bAutoReconnect);
// Add the Content-Type request header.
CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
// Add your API key as a query parameter
CkRestW_AddQueryParam(rest,L"key",L"YOUR_API_KEY");
// The JSON query is contained in the body of the HTTP POST.
// This is a sample query (which we'll dynamically build in this example)
// {
// "homeMobileCountryCode": 310,
// "homeMobileNetworkCode": 260,
// "radioType": "gsm",
// "carrier": "T-Mobile",
// "cellTowers": [
// {
// "cellId": 39627456,
// "locationAreaCode": 40495,
// "mobileCountryCode": 310,
// "mobileNetworkCode": 260,
// "age": 0,
// "signalStrength": -95
// }
// ],
// "wifiAccessPoints": [
// {
// "macAddress": "01:23:45:67:89:AB",
// "signalStrength": 8,
// "age": 0,
// "signalToNoiseRatio": -65,
// "channel": 8
// },
// {
// "macAddress": "01:23:45:67:89:AC",
// "signalStrength": 4,
// "age": 0
// }
// ]
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_AppendInt(json,L"homeMobileCountryCode",310);
CkJsonObjectW_AppendInt(json,L"homeMobileNetworkCode",260);
CkJsonObjectW_AppendString(json,L"radioType",L"gsm");
CkJsonObjectW_AppendString(json,L"carrier",L"T-Mobile");
aCellTowers = CkJsonArrayW_Create();
CkJsonObjectW_AppendArray2(json,L"cellTowers",aCellTowers);
oCellTower = CkJsonObjectW_Create();
CkJsonArrayW_AddObjectAt2(aCellTowers,0,oCellTower);
CkJsonObjectW_AppendInt(oCellTower,L"cellId",39627456);
CkJsonObjectW_AppendInt(oCellTower,L"locationAreaCode",40495);
CkJsonObjectW_AppendInt(oCellTower,L"mobileCountryCode",310);
CkJsonObjectW_AppendInt(oCellTower,L"mobileNetworkCode",260);
CkJsonObjectW_AppendInt(oCellTower,L"age",0);
CkJsonObjectW_AppendInt(oCellTower,L"signalStrength",-95);
aWifi = CkJsonArrayW_Create();
CkJsonObjectW_AppendArray2(json,L"wifiAccessPoints",aWifi);
oPoint = CkJsonObjectW_Create();
CkJsonArrayW_AddObjectAt2(aWifi,0,oPoint);
CkJsonObjectW_AppendString(oPoint,L"macAddress",L"01:23:45:67:89:AB");
CkJsonObjectW_AppendInt(oPoint,L"signalStrength",8);
CkJsonObjectW_AppendInt(oPoint,L"age",0);
CkJsonObjectW_AppendInt(oPoint,L"signalToNoiseRatio",-65);
CkJsonObjectW_AppendInt(oPoint,L"channel",8);
CkJsonArrayW_AddObjectAt2(aWifi,1,oPoint);
CkJsonObjectW_AppendString(oPoint,L"macAddress",L"01:23:45:67:89:AC");
CkJsonObjectW_AppendInt(oPoint,L"signalStrength",4);
CkJsonObjectW_AppendInt(oPoint,L"age",0);
responseJson = CkRestW_fullRequestString(rest,L"POST",L"/geolocation/v1/geolocate",CkJsonObjectW_emit(json));
if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkJsonArrayW_Dispose(aCellTowers);
CkJsonObjectW_Dispose(oCellTower);
CkJsonArrayW_Dispose(aWifi);
CkJsonObjectW_Dispose(oPoint);
return;
}
// When successful, the response code is 200.
if (CkRestW_getResponseStatusCode(rest) != 200) {
// Examine the request/response to see what happened.
wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
wprintf(L"response JSON: %s\n",responseJson);
wprintf(L"---\n");
wprintf(L"LastRequestStartLine: %s\n",CkRestW_lastRequestStartLine(rest));
wprintf(L"LastRequestHeader: %s\n",CkRestW_lastRequestHeader(rest));
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkJsonArrayW_Dispose(aCellTowers);
CkJsonObjectW_Dispose(oCellTower);
CkJsonArrayW_Dispose(aWifi);
CkJsonObjectW_Dispose(oPoint);
return;
}
CkJsonObjectW_putEmitCompact(json,FALSE);
wprintf(L"JSON request body: %s\n",CkJsonObjectW_emit(json));
// The JSON response should look like this:
// {
// "location": {
// "lat": 37.4248297,
// "lng": -122.07346549999998
// },
// "accuracy": 1145.0
// }
wprintf(L"JSON response: %s\n",responseJson);
jsonResp = CkJsonObjectW_Create();
CkJsonObjectW_Load(jsonResp,responseJson);
jsonLoc = CkJsonObjectW_Create();
CkJsonObjectW_ObjectOf2(jsonResp,L"location",jsonLoc);
// Any JSON value can be obtained as a string..
latitude = CkJsonObjectW_stringOf(jsonLoc,L"lat");
wprintf(L"latitude = %s\n",latitude);
longitude = CkJsonObjectW_stringOf(jsonLoc,L"lng");
wprintf(L"longitude = %s\n",longitude);
accuracy = CkJsonObjectW_stringOf(jsonResp,L"accuracy");
wprintf(L"accuracy = %s\n",accuracy);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkJsonArrayW_Dispose(aCellTowers);
CkJsonObjectW_Dispose(oCellTower);
CkJsonArrayW_Dispose(aWifi);
CkJsonObjectW_Dispose(oPoint);
CkJsonObjectW_Dispose(jsonResp);
CkJsonObjectW_Dispose(jsonLoc);
}