AWS CDK: Can't define Content-Type in AWS API Gateway Integration Response

Viewed 445

I'm trying to add a GET Response to my API Gateway with AWS CDK. However, I get this Error when I do cdk deploy:

Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression parameter specified: method.response.header.Content-Type] (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: f4b6ec17-f0ae-4e2a-aa18-b86ddcac807a; Proxy: null)

My Code:

const admin = api.root.addResource('admin');
    admin.addMethod(
      'GET',
      new apigw.LambdaIntegration(hmtl_code, {
        passthroughBehavior: apigw.PassthroughBehavior.WHEN_NO_MATCH,
        requestTemplates: { 'application/json': '{"statusCode": 200}' },
        integrationResponses: [
          {
            statusCode: "200",
            responseParameters: {
              'method.response.header.Content-Type': "'text/html'"
            },
            responseTemplates: { "text/html": "$input.path('$')" }
          }
        ]
      }),
      {
        methodResponses: [
          {
            statusCode: "200",
          }
        ]
      }
    );

I tried it with a MockIntegration as well, but this Error message keeps popping up. Help would be appreciated, thanks :)

1 Answers

Clearly a bit late here, but I just ran into the same issue and wish the cdk error was clearer.

The parameters in integrationResponses needs to be defined as part of methodResponses too.

From the docs for IntegrationResponse.responseParameters:

The destination must be an existing response parameter in the MethodResponse property.

So applying that to your code...

const admin = api.root.addResource('admin');
admin.addMethod(
  'GET',
  new apigw.LambdaIntegration(hmtl_code, {
    passthroughBehavior: apigw.PassthroughBehavior.WHEN_NO_MATCH,
    requestTemplates: { 'application/json': '{"statusCode": 200}' },
    integrationResponses: [
      {
        statusCode: '200',
        responseParameters: {
          'method.response.header.Content-Type': "'text/html'",
        },
        responseTemplates: { 'text/html': "$input.path('$')" },
      },
    ],
  }),
  {
    methodResponses: [
      {
        statusCode: '200',
        responseParameters: {
          // a required response parameter
          'method.response.header.Content-Type': true,
          // an optional response parameter
          'method.response.header.Content-Length': false,
        },
      },
    ],
  }
);
Related