jel*_*ens 3 html python aws-lambda aws-api-gateway
我目前有一个返回 JSON 对象的 Python lambda 函数。然而,我想知道是否有一种方法可以返回一个返回而不是 JSON 的 html 网页,而不是得到带有 JSON 数据的白屏?目前返回数据看起来像
return {
'statusCode': 200,
'headers': {},
'body': json.dumps({
'HOW-TO: ': prompt,
'instanceId': instanceId,
'PUBLIC_IP_ADDRESS': publicIp,
'instanceRegion':Instance_region
})
Run Code Online (Sandbox Code Playgroud)
但很好奇是否有办法返回 HTML 页面?
小智 7
是的,您可以,只需使用 AWS Python 定义正确的标头
“标题”:{'内容类型':'text/html'}
如下例所示(请调整正确的Python标识)
def lambda_handler(事件,上下文):
import json
longinformation = '''
<h1 style="color: #5e9ca0;">You can edit <span style="color: #2b2301;">this demo</span> text!</h1>
<h2 style="color: #2e6c80;">How to send HTML with AWS lambda in Python:</h2>
<p>Paste your documents in the visual editor on the left or your HTML code in the source editor in the right. <br />Edit any of the two areas and see the other changing in real time. </p>
'''
return {
"statusCode": 200,
"headers": {'Content-Type': 'text/html'}, #it really works by Hector Duran!
"body": longinformation
}
Run Code Online (Sandbox Code Playgroud)