Return HTML to browser from API Gateway using Lambda Proxy Integration

0

I have a Lambda function built and deployed which accepts a collection of data in the request, processes and returns an HTML string for presentation to the user's browser. I have the Lambda function working. the function is to be integrated with API gateway. The return to the user is represented as a JSON object.

the python code is

    return {
    "statusCode": 200,
    "body": userResponse
}

where userResponse would contain "<html>...<body>...</body></html>.

I have an API set up in API gateway, which is implemented using Lambda Proxy Integration. It appears all I can do is define a model, but no body mapping template.

When I submit my querystring to the API endpoint, I get a response displayed in my web browser which is the html text as in

<html>
<head>
<meta http-equiv="Content-Type" content="text/html"/>
<meta charset="utf-8" />
</head>
<body>
...
</body>
</html>

the html is not rendered by the browser.

If I save the file to my machine as an html file, it is correctly rendered in the browser.

So, how do I tell API gateway to return html to the web browser with Lambda proxy integration turned on? What does the Model look like? Where does the body mapping happen?

thanks

질문됨 5년 전4331회 조회
1개 답변
1

If you add a content type header to your returned data it should render properly:

return {
"statusCode": 200,
"body": userResponse,
"headers": {"Content-Type": "text/html"}
}

답변함 4년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠