Boris K
Boris K

Reputation: 3580

Returning HTML with Azure Serverless Function req.body

I've got some TIF files in Azure Blob Storage. I'd like to display them in the browser via a link embedded in a spreadsheet. The simplest way to do this should be to take the file code as a request parameter and return the properly formatted HTML, right?

So right now I've got it returning a req.body with some HTML. Unfortunately, the HTML just shows up as a string in the browser. How do I make it render as HTML with minimal rigamarole?

Here's my code:

if (req.query.blob) {
    let blob = req.query.blob;
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: `<object width=200 height=200 
                data="<baseaddress>/${blob}.tif" type="image/tiff">
                <param name="src" value="<baseaddress>/${blob}.tif">
                <param name="negative" value="yes">
                </object>`
    };
}

Upvotes: 2

Views: 855

Answers (1)

Zanon
Zanon

Reputation: 30790

You need to set the headers to specify the content type to HTML and the response must be a full valid HTML page (with the <html> tag and the rest).

Example:

module.exports.hello = (event, context, callback) => {

  const html = `
    <!doctype html>
    <html>
      <head>
        <title>The Page Title</title>
      </head>
      <body>
        <h1>Hello</h1>
      </body>
    </html>`;

  const response = {    
    statusCode: 200, 
    headers: {
      'Content-Type': 'text/html'
    }, 
    body: html
  };

  callback(null, response);
};

Upvotes: 3

Related Questions