Delphi DLL
Delphi DLL
Call an AWS Lambda Function
See more AWS Misc Examples
Demonstrates how to call an AWS Lambda function.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AuthAws, Rest, JsonObject, StringBuilder;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
rest: HCkRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
authAws: HCkAuthAws;
json: HCkJsonObject;
sbRequestBody: HCkStringBuilder;
sbResponseBody: HCkStringBuilder;
statusCode: Integer;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest := CkRest_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 := CkRest_Connect(rest,'itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws',port,bTls,bAutoReconnect);
// Provide AWS credentials for the REST call.
authAws := CkAuthAws_Create();
CkAuthAws_putAccessKey(authAws,'AWS_ACCESS_KEY');
CkAuthAws_putSecretKey(authAws,'AWS_SECRET_KEY');
// the region should match our domain above..
CkAuthAws_putRegion(authAws,'us-west-2');
CkAuthAws_putServiceName(authAws,'lambda');
CkRest_SetAuthAws(rest,authAws);
json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'name','Benny');
CkRest_AddHeader(rest,'Content-Type','application/json');
sbRequestBody := CkStringBuilder_Create();
CkJsonObject_EmitSb(json,sbRequestBody);
sbResponseBody := CkStringBuilder_Create();
success := CkRest_FullRequestSb(rest,'POST','/',sbRequestBody,sbResponseBody);
if (success = False) then
begin
Memo1.Lines.Add(CkRest__lastErrorText(rest));
Exit;
end;
statusCode := CkRest_getResponseStatusCode(rest);
if (statusCode >= 400) then
begin
Memo1.Lines.Add('Response Status Code: ' + IntToStr(statusCode));
Memo1.Lines.Add('Response Body: ' + CkStringBuilder__getAsString(sbResponseBody));
Memo1.Lines.Add('Failed.');
Exit;
end;
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponseBody));
CkRest_Dispose(rest);
CkAuthAws_Dispose(authAws);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbRequestBody);
CkStringBuilder_Dispose(sbResponseBody);
end;