从 AWS API Gateway 返回 HTML [英] Return HTML from AWS API Gateway

查看:47
本文介绍了从 AWS API Gateway 返回 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现与这篇文章相同的目标,但我不明白那里的回复,也无权在评论中要求澄清.

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 中实现),并通过空响应模型返回文本/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 端点使用默认的输出直通行为@集成响应,则引用 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:

"
<html>
<body>
 ... 
</body>
</html>
"

那个答案(@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 Gateway 中,我的集成响应现在使用映射来完全按照建议提取元素(对于 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆