Return HTML

Python example

Returning HTML via a Cloudflare Worker.

def handleRequest(request):


    # defines a html string for the JSON content

        <!DOCTYPE html>
                    function msg() {
                        document.getElementById("content").innerHTML = "Hello World.";
                <h1>Hello World.</h1>  
                    <p>This markup was generated by a Cloudflare Worker.</p>
                    <img src="{}" height="20px" width="20px"/>
                            <input type="button" value="Click me" onclick="msg()">
                    <div id="content">

    # a new Response
    return __new__(Response(html, {
        'headers' : { 'content-type' : 'text/html;charset=UTF-8'}

# call the Worker runtime
addEventListener('fetch', (lambda event: event.respondWith(handleRequest(event.request))))

Which returns the following page.


Photo by Asad Photo Maldives from Pexels

Last updated on 8 Oct 2020
Published on 8 Oct 2020