Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (19.1k points)

I have an API Gateway endpoint that accepts a GET request, passes through some request variables to the Lambda function (implemented in Python), and returns text/html via an Empty Response Model (as described here

As described in the earlier SO question, if the Lambda function returns an HTML string and the API endpoint uses the default Output Passthrough behaviour @ Integration Response, the HTML output is quoted:

"\n<html>\n<body>\n ... \n</body>\n</html>\n"

That answer says "Simply store the HTML markup in a variable and return it.", but I am unsure what that means in the context of a Lambda function. Does it mean to return an "application/json" response with an element named "variableHTML"? Something like this?

"{\"variableHTML\": \"\\n<html>\\n<body>\\n ... \\n</body>\\n</html>\\n\"}"

I set that up & in API Gateway my Integration Response now uses a Mapping to extract the element (for 200 application/json responses) exactly as suggested:

#set($inputRoot = $input.path('$')) 

$inputRoot.variableHTML .

The result is now a single dot being returned.

I have tried many variations ($input.json instead of $input.path, different content types at different stages, etc), but feel the above setup most closely matches the accepted answer from the other thread.

Any insight where I am going wrong with this will be appreciated.

1 Answer

0 votes
by (44.4k points)

Any Python object which is returned will be serialized to JSON. So, to get the value of this string, you have to do Integration Response mapping:

#set($inputRoot = $input.path('$')) 


The #set line gives $inputRoot the value of the entire JSON object your Python program returned... which is just the original string you returned before the Lambda framework converted it to JSON.

Suppose you needed to create the response in the mapping, instead of in your program. Then, rather than returning a Python string, you could return a Python object, like so:

return {"title": "Greeting", "message": "Hello"}

Your mapping can convert that to HTML like so:

#set($inputRoot = $input.path('$')) 



Using a mapping that way is more useful if you are returning structured data than simple HTML, though. I'd use the first mapping above for your issue.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

Browse Categories