I have developed an AWS Lambda function using the AWS Console. The URL for this function is https://mi7ark5tunresgw25fdjstovuq0woylg.lambda-url.us-west-1.on.aws/.
This function can be tested using the AWS Console.
The input JSON for testing is:
{
"Body": "Q0FDREJBQUItOEFBQi00QUFFLTlBQUUtNkFBQUFBQWNkM2U5"
}
The function returns a JSON response.
Here is the C# code for the Lambda function:
public APIGatewayHttpApiV2ProxyResponse FunctionHandler(APIGatewayHttpApiV2ProxyRequest oldKey, ILambdaContext context)
{
JsonSerializerOptions option = new JsonSerializerOptions();
if (oldKey.Body.Length != 48) return (new APIGatewayHttpApiV2ProxyResponse
{
StatusCode = (int)HttpStatusCode.NotAcceptable,
Body = JsonSerializer.Serialize<string>(string.Empty, option),
});
string newKey = string.Empty;
dcLicense.Extend(oldKey.Body, out newKey);
return (new APIGatewayHttpApiV2ProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = JsonSerializer.Serialize<string>(newKey, option),
});
}
I am looking to call this function from a .NET Framework 4.8 application & get the output string.
Is there any simple sample code for this?
If you're looking to trigger your Lambda function over the public internet, which I assume is the case since you've created a (publicly accessible) function URL, you can easily achieve this by making an HTTP request from your .NET Framework application.
When the Lambda function is triggered via the Function URL, the incoming request is a JSON object that is passed to the Lambda. This object contains various details such as the HTTP method, query string values, request body etc. This is what defines what the APIGatewayHttpApiV2ProxyRequest
object will contain, that you've configured your Lambda to accept.
Inside the API Gateway console, you have the ability to define an APIGatewayHttpApiV2ProxyRequest
object to use as a test event. Your test event (or what you refer slightly incorrectly to as the 'input JSON') is essentially a APIGatewayHttpApiV2ProxyRequest
object with its Body
set to Q0F...
.
However, outside of the API Gateway console, we don't have the ability to manually create a test event. Instead, AWS maps the request to the event that your Lambda accepts.
Since you only use the Body
from APIGatewayHttpApiV2ProxyRequest
(oldKey.Body
), all we need to do is pass Q0F...
to the API Gateway. This will then be mapped to create the exact test event you have defined above - a request object with the Body
set to Q0F...
.
Note other fields, like headers, are also set accordingly but since you don't use them, we don't need to set them for the HTTP request.
This will work:
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Program {
public static void Main() {
var lambdaUrl = "https://mi7ark5tunresgw25fdjstovuq0woylg.lambda-url.us-west-1.on.aws/";
var functionInput = "Q0FDREJBQUItOEFBQi00QUFFLTlBQUUtNkFBQUFBQWNkM2U5";
using(HttpClient httpClient = new HttpClient()) {
var response = await httpClient.PostAsync(lambdaUrl, new StringContent(functionInput));
if (response.IsSuccessStatusCode) {
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response: " + responseBody);
} else
Console.WriteLine("Failed to get response with status code: " + response.StatusCode);
}
}
}