Sample code for 30+ languages & platforms
Unicode C

Create a New Table in an Azure Storage Account

See more Azure Table Service Examples

Creates a new table using the Azure Table Service REST API.

Chilkat Unicode C Downloads

Unicode C
#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;
    HCkJsonObjectW jsonResponse;
    const wchar_t *odata_metadata;
    const wchar_t *odata_type;
    const wchar_t *odata_id;
    const wchar_t *odata_editLink;
    const wchar_t *TableName;

    success = FALSE;

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

    rest = CkRestW_Create();

    // Implements the following CURL command:

    // curl -X POST \
    //   -H "Content-Type: application/json" \
    //   -H "Accept: application/json;odata=fullmetadata" \
    //   -H "Prefer: return-content" \
    //   -d '{   
    //     "TableName":"mytable"  
    // }' https://myaccount.table.core.windows.net/Tables

    // Use the following online tool to generate REST code from a CURL command
    // Convert a cURL Command to REST Source Code

    // IMPORTANT: Make sure to change "myaccount" to your actual Azure Storage Account name.
    // URL: https://myaccount.table.core.windows.net/Tables
    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.

    // {
    //   "TableName": "mytable"
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"TableName",L"mytable");

    CkRestW_AddHeader(rest,L"Accept",L"application/json;odata=fullmetadata");
    CkRestW_AddHeader(rest,L"Prefer",L"return-content");
    CkRestW_AddHeader(rest,L"Content-Type",L"application/json");

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,sbRequestBody);
    sbResponseBody = CkStringBuilderW_Create();
    success = CkRestW_FullRequestSb(rest,L"POST",L"/Tables",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;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        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;
    }

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jsonResponse,sbResponseBody);

    CkJsonObjectW_putEmitCompact(jsonResponse,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse));

    odata_metadata = CkJsonObjectW_stringOf(jsonResponse,L"\"odata.metadata\"");
    odata_type = CkJsonObjectW_stringOf(jsonResponse,L"\"odata.type\"");
    odata_id = CkJsonObjectW_stringOf(jsonResponse,L"\"odata.id\"");
    odata_editLink = CkJsonObjectW_stringOf(jsonResponse,L"\"odata.editLink\"");
    TableName = CkJsonObjectW_stringOf(jsonResponse,L"TableName");


    CkRestW_Dispose(rest);
    CkAuthAzureStorageW_Dispose(azAuth);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jsonResponse);

    }