Unable to send xml response with serverless framework

aws-api-gateway, aws-lambda, serverless-framework, twilio, twilio-twiml

Solution

You don't need to mess with serverless.yml so much. Here is the simple way:

In serverless.yml...

functions:
  voice:
    handler: handler.voice
    events:
      - http:
          path: voice
          method: post

(response, headers, Content-Type, template, and statusCodes are not necessary)

Then you can just set the statusCode and Content-Type in your function.

So delete this part...

context.succeed({
    body: twiml.toString()
  });

... and replace it with:

const response = {
    statusCode: 200,
    headers: {
      'Content-Type': 'text/xml',
    },
    body: twiml.toString(),
};

callback(null, response);

Lambda proxy integration (which is the default) assembles it into a proper response.

Personally I find this way simpler and more readable.

Problem

I'm working with twilio in which when call comes to my twilio number it invokes webhook, I'm using lambda function as webhook, twilio expects xml(formerly called twiml) response from webhook and i'm unable to send xml response from lambda function I'm using serverless framework here is my code function: ``` module.exports.voice = (event, context, callback) => { console.log("event", JSON.stringify(event)) var twiml = new VoiceResponse(); twiml.say({ voice: 'alice' }, 'Hello, What type of podcast would you like to listen? '); twiml.say({ voice: 'alice' }, 'Please record your response after the beep. Press any key to finish.'); twiml.record({ transcribe: true, transcribeCallback: '/voice/transcribe', maxLength: 10 }); console.log("xml: ", twiml.toString()) context.succeed({ body: twiml.toString() }); }; ``` yml: ``` service: aws-nodejs provider: name: aws runtime: nodejs6.10 timeout: 10 iamRoleStatements: - Effect: "Allow" Action: "*" Resource: "*" functions: voice: handler: handler.voice events: - http: path: voice method: post integration: lambda response: headers: Content-Type: "'application/xml'" template: $input.path("$") statusCodes: 200: pattern: '.*' # JSON response template: application/xml: $input.path("$.body") # XML return object headers: Content-Type: "'application/xml'" ``` Response: please let me know if I'm making some mistake in code also created an issue on github Thanks, Inzamam Malik

Original source