Back to Home

HTML API Generation on the Server Without Rendering

The article explains why generating HTML text on the server is a basic approach for web services. Examples in Python without dependencies, comparison of HTML API with JSON. Templating engines and the browser's role in rendering are discussed.

HTML as API: server issues text, browser renders
Advertisement 728x90

Generating HTML as an API: A Server That Returns Plain Text

Building a server-side web service is straightforward: respond to HTTP requests with plain text in HTML format. This foundational, reliable approach requires no extra dependencies. The browser handles all the complex rendering work automatically.

Here’s a minimal Python server using the built-in http.server module. It returns a static HTML page for any GET request:

from http.server import BaseHTTPRequestHandler, HTTPServer

WEBPAGE = "<h1>Python webpage!</h1>\n"

class HTMLServer(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(str.encode(WEBPAGE))

webServer = HTTPServer(("localhost", 8080), HTMLServer)
print("Server running at http://localhost:8080")
webServer.serve_forever()

Using curl, you’ll see clean, raw text:

Google AdInline article slot
$ curl localhost:8080
<h1>Python webpage!</h1>

In the browser, the <h1> tag renders as a formatted heading. Add CSS for styling:

WEBPAGE = """
<style>
body {
  background-color: lightblue;
  font-family: 'Comic Sans MS', cursive;
}
</style>
<h1>Python webpage!</h1>
"""

The browser applies styles instantly—no extra steps needed.

Rendering Is the Browser’s Job

The server generates text; the browser parses it into DOM, computes styles, calculates layout, and applies fonts with proper DPI, kerning, and metrics. This multi-stage process includes parsing, style computation, layout, and painting.

Google AdInline article slot

Rendering requires deep knowledge of typography—from converting points to pixels to handling ascender and descender heights. Browsers handle all this complexity for you.

HTML generation is just string manipulation. No need for jargon like "server-side rendering" that implies unnecessary complexity.

Using HTML to Represent Data

Any data can be expressed as HTML through simple string templates. Here’s an example with New York City boroughs:

Google AdInline article slot
boroughs = [
  "The Bronx",
  "Manhattan",
  "Brooklyn",
  "Queens",
  "Staten Island"
]

LIST = "<li>".join(boroughs)
WEBPAGE = "<h1>NYC Boroughs</h1><ul><li>" + LIST + "</ul>"

The browser parses this correctly—even without closing </li> tags (HTML5 is forgiving).

Similarly, for JSON:

LIST = '","'.join(boroughs)
WEBPAGE = '{ "nyc_boroughs": ["' + LIST + '"] }'

HTML wins here: it includes native hypermedia features—links, forms—without requiring client-side JavaScript.

  • Advantages of string-based HTML generation:

- Works on any language without libraries.

- Minimal overhead.

- Easy to debug: curl shows raw output.

- Cross-platform compatibility (bash, awk, Python).

HTML API Over JSON API

Call it an HTML API: the server returns HTML instead of JSON. The browser acts as the client, interpreting hypermedia.

Compare:

HTML:

<h1>NYC Boroughs</h1>
<ul>
  <li>The Bronx
  <li>Manhattan
  <li>Brooklyn
  <li>Queens
  <li>Staten Island
</ul>

JSON:

{
  "nyc_boroughs": [
    "The Bronx",
    "Manhattan",
    "Brooklyn",
    "Queens",
    "Staten Island"
  ]
}

HTML bundles both data and presentation. JSON delivers only data—requiring client-side logic to render.

Template Engines for Production

For real-world services, use template engines like Jinja2. They provide escaping, loops, inheritance, and structure.

Example with Jinja:

<h1>NYC Boroughs</h1>
<ul>
  {% for borough in boroughs %}
  <li>{{ borough }}
  {% endfor %}
</ul>
  • Key benefits of template engines:

1. Automatic HTML escaping to prevent XSS.

2. Reusable fragments (partials).

3. Native syntax for your language (Htmx for Rust, Zig).

4. Stateless design—easy to test and debug.

5. No hydration overhead like in SPAs.

Unlike React, where JSON is hydrated into DOM on the client, templates generate fully rendered HTML on the server.

What Matters Most

  • The server outputs HTML text—the browser handles rendering.
  • An HTML API delivers hypermedia, not raw JSON data.
  • String operations work everywhere, without frameworks.
  • Template engines add security and structure.
  • This approach scales across any programming language.

— Editorial Team

Advertisement 728x90

Read Next