I am trying to achieve the same thing as this post, but I do not understand the reply there and don't have permission to ask for clarification in the comments.
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 behavior @ Integration Response, the HTML output is quoted:
"\n<html>\n<body>\n ... \n</body>\n</html>\n"
That answer (by @ARUNBALAN NV) 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. Thanks for reading!
Overview. In this tuorial you'll first create a Lambda placeholder function that returns HTML and bind it to an API in API Gateway. The placeholder function will return a hard-coded HTML string. Once you have the skeleton in place you can replace this function with a more sophisticated version.
The easiest way to set custom HTTP status code is to setup a Lambda Proxy Integration in API Gateway. In API Gateway > Resource > Actions Dropdown > Create Method > tick Lambda Proxy Integration and select appropriate Lambda function. For async functions just return with an object with statusCode and body .
Amazon API Gateway does not support unencrypted (HTTP) endpoints. By default, Amazon API Gateway assigns an internal domain to the API that automatically uses the Amazon API Gateway certificate. When configuring your APIs to run under a custom domain name, you can provide your own certificate for the domain.
You're very close. The key to understanding this is realizing that whatever Python object you return will be serialized to JSON. So, if you return a string, it will be quoted and escaped to a valid JSON object. If you want the value of this string, then use the following Integration Response mapping:
#set($inputRoot = $input.path('$'))
$inputRoot
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 wanted to build the response in the mapping, instead of in your program. Then, instead of returning a Python string, you could return a Python object, like so:
return {"title": "Greeting", "message": "Hello"}
Your mapping could convert that to HTML like so:
#set($inputRoot = $input.path('$'))
<html><head><title>$inputRoot.title</title></head>
<body>$inputRoot.message</body></html>
Using a mapping that way is more useful if you're returning structured data than simple HTML, though. I'd use the first mapping above for your problem.
When using express, you can just set the header in the app like:
res.set('Content-Type', 'text/html');
When using aws-serverless-express
or similar, those headers should be propagated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With