http Module

The http module offers a complete HTTP/1.1 networking toolkit: an easy-to-use HTTP client (GET, POST, PUT, DELETE, etc.) and a high-performance multithreaded HTTP Server with connection pooling, keep-alive, streaming, and custom routing.

Importing

djazair
use http

HTTP Client Methods

djazair
use http
use json

# Simple GET request
let res = http.get("https://httpbin.org/get")
print("Status: ${res.statusCode}") # => 200
print("Body: ${res.body}")

# POST request with JSON payload
let payload = json.encode({"name": "Djazair", "version": "1.1.0"})
let headers = {"Content-Type": "application/json"}

let postRes = http.post("https://httpbin.org/post", payload, headers)
print("Response status: ${postRes.statusCode}")

Building an HTTP Web Server (Kasbah)

Create a high-performance, non-blocking HTTP server using http.createServer():

djazair
use http
use json

let server = http.createServer(fn(req, res)
    print("${req.method} ${req.path} from ${req.remoteAddr}")

    if req.pathname == "/"
        res.setHeader("Content-Type", "text/html")
        res.status(200).send("<h1>Hello from Djazair Kasbah Server!</h1>")
    elif req.pathname == "/api/status"
        res.setHeader("Content-Type", "application/json")
        res.status(200).send({"status": "running", "uptime": 120})
    else
        res.status(404).send("Page Not Found")
    end
end)

# Start listening on port 8080
print("Server listening on http://localhost:8080")
server.listen(8080)