AWS API Gateway and Python Lambda returning HTML

python lambda return html
html form post to aws api gateway
aws lambda return json
lambda return html from s3
python lambda return headers
aws html forms
aws lambda return url
api gateway - post form-data

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

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',
    }
}

AWS API Gateway and Python Lambda returning HTML, 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  The Lambda Placeholder. Create a simple Lambda function that returns an HTML string. Log into to the AWS Console and navigate to the Lambda service. Create a new Lambda function and select the hello-world template. Name the function lambda-html. Input the code block below to return some basic html.

You have to configure the API Gateway to return the proper Content-Type.

  1. From the API Gateway click on the API you created
  2. Click on "Method Response"
  3. Expand the row for Method response status 200. Click "Add Header" and add a "Content-Type" entry.
  4. Go back to the API you created by clicking "<- Method Execution"
  5. Click on "Integration Response"
  6. Expand the row for Method response status 200
  7. Click "Add mapping template"
  8. Type "text/html" without quotes for the Content-Type and click the checkbox button
  9. In the template area type the JsonPath that maps the part of the json returned from you lambda function to what is returned to the client. For example type $input.path('body') if your json is:

.

{
    "statusCode": 200,
    "body": "<html><body><h1>Test</h1></body></html>,
}
  1. Be sure to deploy the API before testing.

Here's a more detailed article on how to return html from AWS Lambda

Ease the pain of Serverless AWS -How to return HTML from AWS API, Do you need to return an HTML page from your API Gateway & Lambda function​? Are you rendering HTML for web crawlers to read? A Lambda integration maps a path and HTTP method combination to a Lambda function. You can configure API Gateway to pass the body of the HTTP request as-is (custom integration), or to encapsulate the request body in a document that includes all of the request information including headers, resource, path, and method.

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

Return HTML to browser from API Gateway, Return HTML to browser from API Gateway using Lambda Proxy Integration. Posted by: ChrisHare the python code is return { "statusCode":  In the last section we configured a Lambda function that expects its input as a JavaScript Object, and returns a JSON string. We'll use the API Gateway both to accept and respond to the HTTP request, as well as to transform the request/response. Go into AWS and go to API Gateway. Click Create API: Give the API a name, such as example-api. Then click Create API:

Using AWS Lambda with Amazon API Gateway, (custom integration), or to encapsulate the request body in a document that includes all of the request information including headers, resource, path, and method. 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

Set up Lambda proxy integrations in API Gateway, How do you call REST API inside AWS lambda function using python? Python Function on AWS Lambda with API Gateway Endpoint You can get more idea about context object from this aws doc. return type is optional for Create and Configure API Gateway

Return HTML from AWS API Gateway, Any Python object which is returned will be serialized to JSON. So, to get the value of this string, you have to do Integration Response mapping:. Here we look at how to develop a Python code evaluation API with AWS Lambda and API Gateway. It'll allow end users to submit code via an AJAX form submission, which is then executed securely by a Lambda function on AWS.

Comments
  • forgot to mention, running "TEST" works and returns <html><head><title>3DMUVE</title></head> <body><html><body>test</body></html></body></html> but actually hitting the page with the url https:// {my api} .execute-api.us-east-1.amazonaws.com/prod/api/v1/mobs/test results in <html><head></head><pre> <html><head><title>3DMUVE</title></head> <body><html><body>test</body></html></body></html> </pre></body>
  • I guess two things: 1. are you sending the right Accept header and/or Content-Type header to the API and back from the Lambda function? 2. Did you deploy the latest changes to the API?
  • Windsch: the above link in node.js. Is there any way to render HTML page using AWS Lambda function in PYTHON