4

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

4
  • To set the headers, In the Templates area (for a Lambda function) or the Mapping Templates area (for an HTTP proxy or AWS service proxy), next to Content-Type, choose Add (the plus icon). In the Content-Type box, type the content type of the data that will be passed from the method to the Lambda function, HTTP proxy, or AWS service proxy. Then choose Update (the check mark icon). docs.aws.amazon.com/apigateway/latest/developerguide/… Commented Jul 30, 2015 at 14:24
  • This is what I have so where do I set it? screencast.com/t/tvm84Amd Commented Jul 30, 2015 at 15:29
  • Click on either "Method Response" or "Integration Response". You can modify response headers in the resulting screens. Commented Jul 30, 2015 at 15:39
  • 1
    I did but Im still getting this { "Type": "User", "message": "Could not parse request body into json." } When ever I pass any type of parameters in the body Commented Jul 30, 2015 at 15:47

3 Answers 3

5

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


$input.path('$')

Sign up to request clarification or add additional context in comments.

Comments

4

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 ?

2 Comments

Actually I don't like to do that because that means I need to predefine all possible responses in the UI, leaving me little control on the code. So I did this on the mapping: #set($inputRoot = $input.path('$')) #foreach($elem in $inputRoot) $elem #end
Also I felt like an idiot, I was not deploying my API so every change I made was not working. So yeah make sure you deploy when you make changes lol
0

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/

3 Comments

any idea how to do that for the post?
@RukshanDangalla I expect the same technique will work for a POST. You might rather have the POST return a 302 redirect to a GET with this format so the user doesn't suffer the browser asking to repost the form when they hit the back button.
I am trying create facebook canvas app using api gate way and lambda. I successfully created http get to return text/html. But the problem is facebook sending post request to load the page into iframe. Then it shows below message {"message": "Could not parse request body into json: Unrecognized token \'signed_request\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: [B@599d4f6; line: 1, column: 16]"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.