AWSCDK:无法在AWSAPIGateway集成响应中定义Content-Type
创始人
2024-09-24 03:31:26
0

AWS CDK目前不支持在API Gateway集成响应定义中直接设置Content-Type。但是,我们可以通过在Lambda函数中手动设置响应头来达到相同的效果。以下是一个示例Lambda函数,它返回JSON响应并设置Content-Type头:

import { APIGatewayProxyHandler } from 'aws-lambda';

export const handler: APIGatewayProxyHandler = async (event) => {
  const responseBody = {
    message: "Hello, world!"
  };

  return {
    statusCode: 200,
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(responseBody)
  };
};

在CDK中,我们可以使用lambdaIntegration帮助程序包装Lambda函数,并将其集成到API Gateway中:

import * as apigw from '@aws-cdk/aws-apigateway';
import * as lambda from '@aws-cdk/aws-lambda';

// Define the Lambda function
const handler = new lambda.Function(this, 'MyFunction', {
  runtime: lambda.Runtime.NODEJS_14_X,
  code: lambda.Code.fromAsset('path/to/my/function/code'),
  handler: 'index.handler',
});

// Create the API Gateway REST API
const restApi = new apigw.RestApi(this, 'MyRestApi');

// Define the API Gateway integration for the Lambda function
const lambdaIntegration = new apigw.LambdaIntegration(handler);

// Define the API Gateway resource for our endpoint
const resource = restApi.root.addResource('hello');

// Define the API Gateway method for our endpoint
resource.addMethod('GET', lambdaIntegration, {
  // Define any API Gateway method options, such as request / response parameters
  // ...
});

// Set the API Gateway deployment options, such as stage name and description
// ...

通过这种方法,我们可以手动设置Lambda函数的响应头,并将其与API Gateway集成。

相关内容

热门资讯

开挂辅助工具"约局吧... 开挂辅助工具"约局吧能不能开挂"开挂(透视)辅助平台(有挂透视)您好:约局吧能不能开挂这款游戏可以开...
辅助开挂!山西扣点子技巧,aa... 辅助开挂!山西扣点子技巧,aapoker免费透视脚本(透视)开挂辅助插件(竟然有挂);1、让任何用户...
重大通报“wepoker怎么设... 开挂教程视频分享装挂详细步骤在当今的网络游戏中,作为一种经典的娱乐方式,吸引了无数玩家的参与。尤其是...
开挂辅助软件"wep... 开挂辅助软件"wepoker辅助器怎么弄"开挂(透视)辅助脚本(果真有挂);无需打开直接搜索加薇13...
开挂辅助!广东雀神智能辅助照片... 开挂辅助!广东雀神智能辅助照片,wepoker怎么提高运气(透视)开挂辅助插件(真的有挂);广东雀神...
六分钟了解“wepoker免费... 六分钟了解“wepoker免费辅助器”开挂(透视)辅助插件(必备教程有挂细节)>>您好:软件加薇13...
开挂辅助插件"wep... 开挂辅助插件"wepoker透视最简单三个步骤"开挂(透视)辅助工具(有挂教学)您好:wepoker...
辅助开挂!崇阳斗棋辅助脚本视频... 辅助开挂!崇阳斗棋辅助脚本视频,聚星ai辅助工具收费多少(透视)开挂辅助下载(有挂教程);人气非常高...
科普分享“wpk辅助是什么”开... 科普分享“wpk辅助是什么”开挂(透视)辅助脚本(AI教程有挂方略) 了解更多开挂安装加(13670...
开挂辅助软件"wpk... 开挂辅助软件"wpk真吗"开挂(透视)辅助脚本(有挂细节);亲,wpk真吗这款游戏原来确实可以开挂的...