0
votes

Trying to resolve the Twilio 12300 Error: Invalid Content-Type caused when deploying a Lambda (Serverless) app using AWS's API Gateway (GET) and CloudFormation via the aws cli and Swagger.

I have the Integration Response Body Mapping Template > Content-Type set to application/xml and the template

#set($inputRoot = $input.path('S'))
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Message>
    <Body>
      $inputRoot
    </Body>
  </Message>
</Response>

Examining the actual Response Body from Twilio logs, the output matches the template, returning an appropriately formatted XML response.

However, the Response Header > Content-Type still returns application/json.

It seems as if this requires a setting change within Method Response, but either the AWS API Gateway dashboard and/or the Swagger API doesn't allow for me to set the Header > Content-Type to application/xml.

Below is the swagger.yaml file

---
swagger: "2.0"
info:
  version: "2016-12-20T18:27:47Z"
  title: "twilio-apigateway"
basePath: "/Prod"
schemes:
- "https"
paths:
  /addphoto:
    get:
      consumes:
      - "application/json"
      produces:
      - "application/xml"
      responses:
        200:
          description: "200 response"
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
            responseTemplates:
              application/xml: "#set($inputRoot = $input.path('$'))\n<?xml version=\"\
                1.0\" encoding=\"UTF-8\"?>\n<Response>\n    <Message>\n        <Body>\n\
                \            $inputRoot\n        </Body>\n    </Message>\n</Response> "
        requestTemplates:
          application/json: "{\n    \"body\" : \"$input.params('Body')\",\n    \"\
            fromNumber\" : \"$input.params('From')\",\n    \"image\" : \"$input.params('MediaUrl0')\"\
            ,\n    \"numMedia\" : \"$input.params('NumMedia')\"\n}"
        # NOTE: Replace <<region>> and <<account>> fields
        uri: arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:<<cccount_id>>:function:${stageVariables.LambdaFunctionName}/invocations
        passthroughBehavior: "when_no_templates"
        httpMethod: "POST"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws"

Would love some help here from anyone who has resolved this issue. Although it seems this is a known issue (Twilio requiring XML format) and the AWS Swagger API being limited in some ways, which may result in an unresolvable state.

Additional references:

http://docs.aws.amazon.com/fr_fr/apigateway/latest/developerguide/api-gateway-swagger-extensions.html

http://boto3.readthedocs.io/en/latest/reference/services/apigateway.html

1
you're using api gateway as an http proxy integration mapped to a twilio endpoint directly?Dave Maple
yes @dave-maple , the API Gateway endpoint is set as the GET response for the Twilio phone number. So phone_number > api_gateway > lambda (dynamodb + s3) > api_gateway > phone_number.Andrew Stroup
Have you already tried a method response 'Content-Type' definition and a hard-coded integration response header?Dave Maple
If it's of any help, Twilio won't be sending you JSON to your URL (I notice it's set to consume application/json too). If you've requested it to be a GET request the parameters will come as URL query parameters.philnash

1 Answers

3
votes

Have you tried a method response and integration response?

    "responses": {
      "200": {
        "description": "200 response",
        "schema": {
          "$ref": "#/definitions/Empty"
        },
        "headers": {
          "Content-Type": {
            "type": "string"
          }
        }
      }
    },
    "x-amazon-apigateway-integration": {
      "responses": {
        "default": {
          "statusCode": "200",
          "responseParameters": {
            "method.response.header.Content-Type": "'application/xml'"
          }
        }
    }