问题描述
我正在尝试与这篇文章 a>,但我不明白那里的答复,也无权在评论中要求澄清.
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.
我有一个API网关端点,该端点接受GET请求,将一些请求变量传递给Lambda函数(在Python中实现),然后通过空响应模型返回text/html(如此处
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
如前面的SO问题中所述,如果Lambda函数返回HTML字符串,并且API端点使用默认的Output Passthrough行为@ Integration Response,则会引用HTML输出:
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"
(@ ARUNBALAN NV的)答案是仅将HTML标记存储在变量中并返回.",但是我不确定在Lambda函数的上下文中这意味着什么.这是否意味着返回带有名为"variableHTML"的元素的"application/json"响应?像这样吗?
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\"}"
我将其设置为&在API网关中,我的集成响应现在完全按照建议使用映射来提取元素(对于200个应用程序/json响应):
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 .
现在返回一个点.
我尝试了许多变体(用$ input.json代替$ input.path,在不同阶段使用不同的内容类型,等等),但是感觉上面的设置与其他线程接受的答案最接近.
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!
推荐答案
您非常亲密.理解这一点的关键是要意识到,您返回的任何Python对象都将被序列化为JSON.因此,如果您返回一个字符串,它将被引用并转义为有效的JSON对象.如果需要此字符串的值,请使用以下集成响应"映射:
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
#set
行为$inputRoot
提供Python程序返回的整个JSON对象的值...这只是您在Lambda框架将其转换为JSON之前返回的原始字符串.
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.
假设您想在映射中而不是在程序中构建响应.然后,您可以返回一个Python对象,而不是返回Python字符串,如下所示:
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"}
您的映射可以像这样将其转换为HTML:
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>
但是,如果要返回结构化数据,则使用这种方式的映射比简单的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.
这篇关于从AWS API Gateway返回HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!