Return HTML from AWS API Gateway

社会主义新天地 提交于 2019-12-28 09:18:22

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/33614198/return-html-from-aws-api-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!