简体   繁体   English

是否可以使用“HTTP”协议(不是 REST)通过 AWS API 网关返回 HTML?

[英]Is it possible to return HTML with AWS API Gateway using the “HTTP” protocol (not REST)?

Is it possible to return HTML with AWS API Gateway using the "HTTP API" protocol?是否可以使用“HTTP API”协议通过 AWS API 网关返回 HTML?

I have a lambda python function to return some HTML.我有一个 lambda python function 返回一些 Z4C4AD5FCA2E7A3F74DBB1CED00381AA4

import json

def lambda_handler(event, context):
    html = '<!DOCTYPE html><html><head><title>HTML from HTTP API Gateway/Lambda</title></head>' 
    html += '<body><h1>HTML from HTTP API Gateway/Lambda</h1></body></html>'
    
    return {
        'statusCode': 200,
        'body': html
    }

The HTTP API Gateway is set up as default. HTTP API 网关设置为默认值。

The result in the browser is the HTML not interpreted.浏览器中的结果是 HTML 未解释。

<!DOCTYPE html><html><head><title>HTML from HTTP API Gateway/Lambda</title></head><body><h1>HTML from HTTP API Gateway/Lambda</h1></body></html>

I assume it has to do with the header "Content-type" which shows up as "text/plain" but I can't seem to find where to change this, if possible.我认为它与 header “内容类型”有关,它显示为“文本/纯文本”,但如果可能的话,我似乎找不到在哪里更改它。

I was able to do with HTTP API as well, I just added header in the lambda response like below:我也可以使用HTTP API ,我刚刚在 Z945F3FC4492868A73B6CZF 响应中添加了 header

    import json

def lambda_handler(event, context):
    html = '<!DOCTYPE html><html><head><title>HTML from HTTP API Gateway/Lambda</title></head>' 
    html += '<body><h1>HTML from HTTP API Gateway/Lambda</h1></body></html>'
    
    return {
        'statusCode': 200,
        'body': html,
        "headers": {"Content-type": "text/html"}
    }

在此处输入图像描述

There is more information how you can handle your responses directly from your lambda Working with AWS Lambda proxy integrations for HTTP APIs有更多信息可以直接从lambda处理响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM