I'm currently attempting to serve a very basic static webpage when a lambda function is invoked through it's function URL (not through API Gateway). However, when I return the response for the request, it is still being rendered as text by the browser.
I am using SST for orchestration, with the lambda handler written in TypeScript (Node.js). Lambda handler code:
export const handler = async (event: any, context: any) => {
console.log("Handler invoked");
let content = `
<\!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello world</h1>
<p>Hello from my example</p>
</body>
</html>
`;
let response = {
status: 200,
headers: {
"Content-Type": "text/html",
},
body: content,
};
return response;
};
I'm currently attempting to serve a very basic static webpage when a lambda function is invoked through it's function URL (not through API Gateway). However, when I return the response for the request, it is still being rendered as text by the browser.
I am using SST for orchestration, with the lambda handler written in TypeScript (Node.js). Lambda handler code:
export const handler = async (event: any, context: any) => {
console.log("Handler invoked");
let content = `
<\!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello world</h1>
<p>Hello from my example</p>
</body>
</html>
`;
let response = {
status: 200,
headers: {
"Content-Type": "text/html",
},
body: content,
};
return response;
};
And the sst.config.ts:
new sst.aws.Function("UploadPage", {
handler: "src/lambda/lambda.handler",
url: true,
});
When invoked, the browser response looks like this in Chrome:
And like this when invoked in firefox:
I have noted that chrome appears to be showing a lot of escaped characters (particularly of note under the Content-Type header) which led me to believe this was the issue, however firefox doesn't appear to show this and also doesn't render the HTML as expected.
What do i need to change within my code to make the browsers understood that they need to render the code?
Share Improve this question asked Feb 7 at 13:34 carbarettacarbaretta 3241 silver badge6 bronze badges 2 |1 Answer
Reset to default 0Very simple fix for this, which isn't mentioned anywhere in documentation. When returning the response, you need statusCode
, not status
.
Using the following response instead resolved the issue:
let response = {
statusCode: 200,
headers: {
"Content-Type": "text/html",
},
body: content,
};
body
property in your JSON, and make the browser render only that. – C3roe Commented Feb 7 at 13:45