ecorvo
ecorvo

Reputation: 3629

AWS Lambda send response to API Gateway

I have a lambda function that generates some text. This is for a simple Twilio app

<Say>Welcome to your conference room!</Say>
<Dial>
   <Conference beep="true">waitingRoom</Conference>
</Dial>

When I make a POST request using postman it outputs exactly that. but I have two problems:

  1. The headers comes back at application/json, and I need it as text/xml.
  2. When I make the POST request from Twilio I get 502 Bad Gateway

I know it has to do something with the incoming params mapping and also mapping the response from Lambda back to the API Gateway as text/xml. But I can;t figure out how to do this.

enter image description hereenter image description here

Upvotes: 4

Views: 6260

Answers (3)

kennbrodhagen
kennbrodhagen

Reputation: 4348

To modify the Content-Type in the response you need to configure 2 areas in API Gateway: Method Response and Integration Response.

In Method Response, you add the Content-Type response header for your 200 status code.

In Integration Response, you open the 200 response and set the value of Content-Type to text/xml.

Here are a couple of screenshots from an article I wrote on returning html content. Your case sounds very similar in that you want to return a string type that happens to contain xml and have the right Content-Type header sent.

Method Response: Method Response

Integration Response: Integration Response

Here is a link to the original article: http://kennbrodhagen.net/2016/01/31/how-to-return-html-from-aws-api-gateway-lambda/

Upvotes: 0

Clearly
Clearly

Reputation: 1624

I used the following template mapping to basically just strip the quotes and it worked:


$input.path('$')

Upvotes: 5

Francis Toth
Francis Toth

Reputation: 1685

I am glad not to be the only one struggling with AWS Api Gateway :)

As far as I know, AWS Api Gateway is mostly JSON oriented. If you can change the content of the response returned (using JSON), maybe you could resolve your problem :

{"say": "Welcome to your conference room!",
 "dial": [{
        "conference": [{
                "beep": "true",
                "name": "waitingRoom"
        }]
    }
]}

You could then map this content using the mapping template feature (in the Integration response screen), by adding a template with a content-type set to "application/json", and a mapping-template set to :

<Say>$input.json('say')</Say>
<Dial>
    <Conference beep="$input.json('dial.conference.beep')">$input.json('dial.conference.name')</Conference>
</Dial>

Does this help you or I am missing something ?

Upvotes: 4

Related Questions