Unicode C
Unicode C
Azure Table Insert Entity
See more Azure Table Service Examples
Insert an entity into an Azure table.Chilkat Unicode C Downloads
#include <C_CkRestW.h>
#include <C_CkAuthAzureStorageW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkRestW rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkAuthAzureStorageW azAuth;
HCkJsonObjectW json;
HCkStringBuilderW sbRequestBody;
HCkStringBuilderW sbResponseBody;
int respStatusCode;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest = CkRestW_Create();
// IMPORTANT: Make sure to change "myaccount" to your actual Azure Storage Account name.
// IMPORTANT: Also change "mytable" to the name of your Azure table.
// We're going to POST to this URL: https://myaccount.table.core.windows.net/mytable
bTls = TRUE;
port = 443;
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"myaccount.table.core.windows.net",port,bTls,bAutoReconnect);
if (success != TRUE) {
wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
return;
}
// Provide Azure Cloud credentials for the REST call.
azAuth = CkAuthAzureStorageW_Create();
CkAuthAzureStorageW_putAccessKey(azAuth,L"AZURE_ACCESS_KEY");
// The account name used here should match the 1st part of the domain passed in the call to Connect (above).
CkAuthAzureStorageW_putAccount(azAuth,L"myaccount");
CkAuthAzureStorageW_putScheme(azAuth,L"SharedKey");
CkAuthAzureStorageW_putService(azAuth,L"Table");
// This causes the "x-ms-version: 2019-07-07" header to be automatically added.
CkAuthAzureStorageW_putXMsVersion(azAuth,L"2019-07-07");
success = CkRestW_SetAuthAzureStorage(rest,azAuth);
// Note: The application does not need to explicitly set the following
// headers: Content-Length, x-ms-date, Authorization. These headers
// are automatically set by Chilkat.
// Note: The above code does not need to be repeatedly called for each REST request.
// The rest object can be setup once, and then many requests can be sent. Chilkat will automatically
// reconnect within a FullRequest* method as needed. It is only the very first connection that is explicitly
// made via the Connect method.
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "PartitionKey":"mypartitionkey",
// "RowKey":"myrowkey",
// "Timestamp":"2013-08-22T01:12:06.2608595Z",
// "Address":"Mountain View",
// "Age":23,
// "AmountDue":200.23,
// "CustomerCode":"c9da6455-213d-42c9-9a79-3e9149a57833",
// "CustomerSince":"2008-07-10T00:00:00",
// "IsActive":true,
// "NumberOfOrders":"255"
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"PartitionKey",L"mypartitionkey");
CkJsonObjectW_UpdateString(json,L"RowKey",L"myrowkey");
CkJsonObjectW_UpdateString(json,L"Timestamp",L"2013-08-22T01:12:06.2608595Z");
CkJsonObjectW_UpdateString(json,L"Address",L"Mountain View");
CkJsonObjectW_UpdateInt(json,L"Age",23);
CkJsonObjectW_UpdateNumber(json,L"AmountDue",L"200.23");
CkJsonObjectW_UpdateString(json,L"CustomerCode",L"c9da6455-213d-42c9-9a79-3e9149a57833");
CkJsonObjectW_UpdateString(json,L"CustomerSince",L"2008-07-10T00:00:00");
CkJsonObjectW_UpdateBool(json,L"IsActive",TRUE);
CkJsonObjectW_UpdateString(json,L"NumberOfOrders",L"255");
// IMPORTANT: Pay attention to the options for nometadata, minimalmetadata, or fullmetadata.
// See the Azure table service API documentation at https://docs.microsoft.com/en-us/rest/api/storageservices/insert-entity
CkRestW_AddHeader(rest,L"Accept",L"application/json;odata=nometadata");
CkRestW_AddHeader(rest,L"Prefer",L"return-no-content");
CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
sbRequestBody = CkStringBuilderW_Create();
CkJsonObjectW_EmitSb(json,sbRequestBody);
sbResponseBody = CkStringBuilderW_Create();
// IMPORTANT: Change "mytable" to the name of your actual table.
success = CkRestW_FullRequestSb(rest,L"POST",L"/mytable",sbRequestBody,sbResponseBody);
if (success != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
CkAuthAzureStorageW_Dispose(azAuth);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbRequestBody);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
// A status code of 204 is a success response for the case where Prefer=return-no-content.
respStatusCode = CkRestW_getResponseStatusCode(rest);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkRestW_responseHeader(rest));
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));
CkRestW_Dispose(rest);
CkAuthAzureStorageW_Dispose(azAuth);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbRequestBody);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
wprintf(L"Success.\n");
CkRestW_Dispose(rest);
CkAuthAzureStorageW_Dispose(azAuth);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbRequestBody);
CkStringBuilderW_Dispose(sbResponseBody);
}