XDPXI's Documentation
HTTP

Examples

Required version: Sponge >=1.0.5

Basic Static Site

The simplest multi-page website:

use http;

def main() {
    http.page("/", "<h1>Home</h1><p>Welcome!</p>");
    http.page("/about", "<h1>About</h1><p>Learn about us.</p>");
    http.page("/contact", "<h1>Contact</h1><p>Email: hello@example.com</p>");
    
    http.serve(5000);
}

Using Templates

Load HTML from separate files:

use http;

def main() {
    let home = http.template("templates/home.html");
    let about = http.template("templates/about.html");
    let services = http.template("templates/services.html");
    
    http.page("/", home);
    http.page("/about", about);
    http.page("/services", services);
    
    http.serve(3000);
}

JSON API

Build a REST API with JSON responses:

use http;

def get_users() {
    let users = [
        { id: 1, name: "Alice", email: "alice@example.com" },
        { id: 2, name: "Bob", email: "bob@example.com" },
        { id: 3, name: "Charlie", email: "charlie@example.com" }
    ];
    return http.json(users);
}

def get_status() {
    let response = {
        status: "running",
        version: "1.0.0",
        uptime: 3600
    };
    return http.json(response);
}

def main() {
    http.page("/api/users", get_users());
    http.page("/api/status", get_status());
    
    http.serve(5000);
}

Dynamic HTML

Generate HTML dynamically with functions:

use http;

def render_page(title, content) {
    let html = "<html><head><title>" + title + "</title></head>";
    html = html + "<body><h1>" + title + "</h1>";
    html = html + "<p>" + content + "</p></body></html>";
    return html;
}

def main() {
    http.page("/", render_page("Home", "Welcome!"));
    http.page("/services", render_page("Services", "Web development."));
    http.page("/blog", render_page("Blog", "Check back soon!"));
    
    http.serve(5000);
}

Building Lists

Generate HTML lists dynamically:

use http;

def render_list(items) {
    let html = "<ul>";
    for item in items {
        html = html + "<li>" + item + "</li>";
    }
    html = html + "</ul>";
    return html;
}

def main() {
    let menu = ["Home", "About", "Services", "Contact"];
    let nav = render_list(menu);
    
    http.page("/", "<h1>Navigation</h1>" + nav);
    http.serve(5000);
}

Data Processing

Process and transform data for API responses:

use http;

def calculate_stats(numbers) {
    let total = 0;
    for num in numbers {
        total = total + num;
    }
    let average = total / len(numbers);
    return {
        count: len(numbers),
        total: total,
        average: average
    };
}

def get_stats() {
    let data = [10, 20, 30, 40, 50];
    let stats = calculate_stats(data);
    return http.json(stats);
}

def main() {
    http.page("/api/stats", get_stats());
    http.serve(8000);
}

Organizing Routes

Structure larger applications with separate route functions:

use http;

# HTML page routes
def get_home() {
    return http.template("templates/home.html");
}

def get_about() {
    return http.template("templates/about.html");
}

# API routes
def get_users() {
    let users = [
        { id: 1, name: "Alice" },
        { id: 2, name: "Bob" }
    ];
    return http.json(users);
}

def get_products() {
    let products = [
        { id: 1, name: "Widget", price: 9.99 },
        { id: 2, name: "Gadget", price: 19.99 }
    ];
    return http.json(products);
}

def main() {
    # HTML pages
    http.page("/", get_home());
    http.page("/about", get_about());
    
    # API endpoints
    http.page("/api/users", get_users());
    http.page("/api/products", get_products());
    
    http.serve(5000);
}

Dashboard

A simple dashboard with multiple data sources:

use http;

def get_dashboard() {
    let stats = {
        users: 150,
        products: 45,
        orders: 328,
        revenue: 15750.50
    };
    
    let html = "<h1>Dashboard</h1>";
    html = html + "<p>Users: " + stats["users"] + "</p>";
    html = html + "<p>Products: " + stats["products"] + "</p>";
    html = html + "<p>Orders: " + stats["orders"] + "</p>";
    html = html + "<p>Revenue: $" + stats["revenue"] + "</p>";
    
    return html;
}

def main() {
    http.page("/", get_dashboard());
    http.serve(5000);
}

Making HTTP Requests

Fetch data from external APIs:

use http;

def get_external_data() {
    let response = http.get("http://api.example.com/data");
    return response;
}

def main() {
    http.page("/api/external", get_external_data());
    http.serve(5000);
}

Posting to an External API

Send data to a remote server:

use http;

def create_user(name, email) {
    let data = { name: name, email: email };
    let json = http.json(data);
    let response = http.post("http://api.example.com/users", json);
    return response;
}

def main() {
    let result = create_user("Alice", "alice@example.com");
    http.page("/create", result);
    http.serve(5000);
}

Webhook Forwarding

Forward requests to another service:

use http;

def receive_webhook(payload) {
    let response = http.post("http://backend.example.com/webhook", payload);
    return http.json({ received: true, forwarded: true });
}

def main() {
    http.page("/webhook", receive_webhook("data"));
    http.serve(5000);
}