Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway and Python Lambda returning HTML

I'm trying to return a webpage from my python lambda ftn, using API GW. Instead, I'm getting my page embeded in a tag within the body, instead of the return value being the full page ( header, body, etc... without the pre>

Any suggestions what I might be doing wrong

Thanks

like image 361
user2615236 Avatar asked Dec 04 '16 20:12

user2615236


People also ask

Can API gateway serve HTML?

We can now configure a simple API gateway endpoint to use the above lambda function to output static html. From API gateway, click on Create API. Name the new API as htmldemo. From Actions menu, click on Create Method and select GET.

How do I return custom HTTP status codes from a Lambda function in Amazon API gateway?

The easiest way to set custom HTTP status code is to setup a Lambda Proxy Integration in API Gateway. In API Gateway > Resource > Actions Dropdown > Create Method > tick Lambda Proxy Integration and select appropriate Lambda function. For async functions just return with an object with statusCode and body .


2 Answers

The <pre> tag you are seeing is the browser trying to show you the text returned from server. It is not part of the returned from the Lambda function.

To get it working you need to get the lambda function set the response HTTP header with Content-Type: 'text/html'

for example:

response = {
    "statusCode": 200,
    "body": content,
    "headers": {
        'Content-Type': 'text/html',
    }
}
like image 175
David Lin Avatar answered Sep 22 '22 13:09

David Lin


try: response_body = "<HTML><Title>Title</Title></HTML>"

finally:

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

This just code illustration of David Lin answer

like image 22
Wageeshwara Ranasinghe Avatar answered Sep 21 '22 13:09

Wageeshwara Ranasinghe