Sample code for 30+ languages & platforms
Unicode C

Call an AWS Lambda Function

See more AWS Misc Examples

Demonstrates how to call an AWS Lambda function.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkRestW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAwsW authAws;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    HCkStringBuilderW sbResponseBody;
    int statusCode;

    success = FALSE;

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

    rest = CkRestW_Create();

    // Connect to the Amazon AWS REST server.
    // such as https://email.us-west-2.amazonaws.com/
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;

    // -------------------------------------------------------------------------------------------
    // Note: The source of the lambda function (hosted on AWS) is shown at the bottom of this page.
    // --------------------------------------------------------------------------------------------

    // If your lambda function URL is: https://itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws/
    // then use just the domain part here:
    success = CkRestW_Connect(rest,L"itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws",port,bTls,bAutoReconnect);

    // Provide AWS credentials for the REST call.
    authAws = CkAuthAwsW_Create();
    CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
    CkAuthAwsW_putSecretKey(authAws,L"AWS_SECRET_KEY");
    // the region should match our domain above..
    CkAuthAwsW_putRegion(authAws,L"us-west-2");
    CkAuthAwsW_putServiceName(authAws,L"lambda");

    CkRestW_SetAuthAws(rest,authAws);

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"name",L"Benny");

    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"/",sbRequestBody,sbResponseBody);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    statusCode = CkRestW_getResponseStatusCode(rest);
    if (statusCode >= 400) {
        wprintf(L"Response Status Code: %d\n",statusCode);
        wprintf(L"Response Body: %s\n",CkStringBuilderW_getAsString(sbResponseBody));
        wprintf(L"Failed.\n");
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));


    CkRestW_Dispose(rest);
    CkAuthAwsW_Dispose(authAws);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkStringBuilderW_Dispose(sbResponseBody);

    }