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 <CkRestW.h>
#include <CkAuthAwsW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkRestW rest;

    // Connect to the Amazon AWS REST server.
    // such as https://email.us-west-2.amazonaws.com/
    bool bTls = true;
    int port = 443;
    bool 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 = rest.Connect(L"itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws",port,bTls,bAutoReconnect);

    // Provide AWS credentials for the REST call.
    CkAuthAwsW authAws;
    authAws.put_AccessKey(L"AWS_ACCESS_KEY");
    authAws.put_SecretKey(L"AWS_SECRET_KEY");
    // the region should match our domain above..
    authAws.put_Region(L"us-west-2");
    authAws.put_ServiceName(L"lambda");

    rest.SetAuthAws(authAws);

    CkJsonObjectW json;
    json.UpdateString(L"name",L"Benny");

    rest.AddHeader(L"Content-Type",L"application/json");

    CkStringBuilderW sbRequestBody;
    json.EmitSb(sbRequestBody);

    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestSb(L"POST",L"/",sbRequestBody,sbResponseBody);
    if (success == false) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int statusCode = rest.get_ResponseStatusCode();
    if (statusCode >= 400) {
        wprintf(L"Response Status Code: %d\n",statusCode);
        wprintf(L"Response Body: %s\n",sbResponseBody.getAsString());
        wprintf(L"Failed.\n");
        return;
    }

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",sbResponseBody.getAsString());
    }