#How can i return html in a API route

1 messages · Page 1 of 1 (latest)

ashen crestBOT
#

Hi @kind dock. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To return HTML in an API route, you can create a response with the Content-Type header set to "text/html". Here's an example:

// Example: src/pages/api/some-html.js
export async function get(params, request) {
  const htmlContent = `
    <!DOCTYPE html>
    <html>
      <head>
        <title>Example HTML</title>
      </head>
      <body>
        <h1>Hello, World!</h1>
      </body>
    </html>
  `;

  return new Response(htmlContent, {
    status: 200,
    headers: {
      "Content-Type": "text/html",
    },
  });
}

In this example, we create an API route at src/pages/api/some-html.js that returns an HTML response when accessed. The Content-Type header is set to "text/html" to indicate that the response contains HTML content.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: