要在AWS.NET Lambda SDK中的APIGatewayHttpApiV2ProxyResponse类中添加自定义标题到最终的HTTP响应中,你可以使用以下代码示例:
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
public class Function
{
public APIGatewayHttpApiV2ProxyResponse FunctionHandler(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context)
{
var response = new APIGatewayHttpApiV2ProxyResponse
{
StatusCode = 200,
Body = "Hello from Lambda!",
Headers = new Dictionary
{
{ "Content-Type", "text/plain" },
{ "Custom-Header", "Custom Value" } // 添加自定义标题
}
};
return response;
}
}
在上面的示例中,我们创建了一个APIGatewayHttpApiV2ProxyResponse对象,并设置了状态码、响应主体和标题。你可以将自定义标题添加到Headers属性中,以键值对的形式进行设置。在这个例子中,我们将添加了一个名为"Custom-Header"的自定义标题,并将其值设置为"Custom Value"。
这样,在Lambda函数执行完毕后,API Gateway将会收到一个带有自定义标题的HTTP响应。