3

I have the following lambda function, with an API Gateway trigger point:

def lambda_handler(event, context):
resp = {
    "statusCode": 200,
    "headers": {
        "Access-Control-Allow-Origin": "*",
    },
    "body": "Hello, World!"
}

return resp

When I navigate to the API endpoint, I expected to only see the text "Hello, World!". Instead, I see the entire JSON response. How do I change this function so that It interprets the headers and status code as such, and not as content to render in the browser?

4 Answers 4

2

Assuming your API Gateway is using Lambda Proxy integration, just add content-type: text/html to your response.

Sign up to request clarification or add additional context in comments.

1 Comment

Specifically, in the headers dictionary of your response
1

This is because of the way the lambda is configured with the lambda function. Looks like it is configured as a Lambda Proxy. If you want to see the entire JSON as the output, just disable the Use Lambda Proxy integration. You can find this configuration in the API Gateway console.

Enable the Use Lambda Proxy integration in the API Gateway console to see only the 'Hello, World!'

Comments

0

One option is to use this function setting up the lambda proxy integration option from the API gateway. It will send you back only the body. You can set up quickly. Here is the official link. link

Comments

-1

Lambda and API Gateway aren't supposed to be used as web servers returning HTML. They aren't even optimized for that, it would be waste of resources.

What you should do is have your HTML hosted somewhere else, use JavaScript to communicate with your API, get dynamic data, and then manipulate HTML based on the API responses.

In case you'd like a serverless approach for static HTML hosting, I recommend S3 + CloudFront.

2 Comments

This answer misses the point of the question, which was why was API Gateway returning the entire JSON object to thr client instead of interpreting it to generate an HTTP response with the provided body, headers, and status code. (This occurs if you neglect to use the Lambda proxy integration feature.)
Although you are right, I still consider worthwhile to provide a comprehensive view, instead of answering without any consideration to context and implications of a decision. There was already an answer for what you indicated, but it hadn't been accepted. I don't see any harm from posting a suggestion of alternative architecture and usage of a cloud resource, as I did...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.