Runtime HTTP Library
HTTP server route registration and response helpers.
Available Functions
| Function | Description |
|---|---|
get(path, handler) |
Register a GET route |
post(path, handler) |
Register a POST route |
put(path, handler) |
Register a PUT route |
delete(path, handler) |
Register a DELETE route |
route(path, handler, methods=[]) |
Register route for multiple methods |
middleware(handler) |
Register global middleware |
static(path, directory) |
Register static file serving |
json(status_code, data) |
Create JSON response |
html(status_code, content) |
Create HTML response |
text(status_code, content) |
Create text response |
redirect(url, status_code=302) |
Create redirect response |
error(status_code, message) |
Create error response |
Setup
import "github.com/paularlott/scriptling/extlibs"
// Register the HTTP sub-library
extlibs.RegisterRuntimeHTTPLibrary(p)Route Registration
scriptling.runtime.http.get(path, handler)
Register a GET route.
Parameters:
path(string): URL path (e.g., “/api/users”)handler(string): Handler function as “library.function”
scriptling.runtime.http.post(path, handler)
Register a POST route.
scriptling.runtime.http.put(path, handler)
Register a PUT route.
scriptling.runtime.http.delete(path, handler)
Register a DELETE route.
scriptling.runtime.http.route(path, handler, methods=[])
Register a route for multiple HTTP methods.
Parameters:
path(string): URL pathhandler(string): Handler functionmethods(list, optional): HTTP methods (default: all)
scriptling.runtime.http.middleware(handler)
Register global middleware.
Parameters:
handler(string): Middleware function
The middleware receives the request and should return:
Noneto continue to the handler- A response dict to short-circuit
scriptling.runtime.http.static(path, directory)
Register static file serving.
Parameters:
path(string): URL path prefix (e.g., “/assets”)directory(string): Local directory to serve
Response Helpers
scriptling.runtime.http.json(status_code, data)
Create a JSON response.
Parameters:
status_code(int): HTTP status codedata: Data to serialize as JSON
Returns: Response dict
scriptling.runtime.http.html(status_code, content)
Create an HTML response.
Parameters:
status_code(int): HTTP status codecontent(string): HTML content
Returns: Response dict
scriptling.runtime.http.text(status_code, content)
Create a plain text response.
Parameters:
status_code(int): HTTP status codecontent(string): Text content
Returns: Response dict
scriptling.runtime.http.redirect(location, status=302)
Create a redirect response.
Parameters:
location(string): URL to redirect tostatus(int, optional): HTTP status code (default: 302)
Returns: Response dict
scriptling.runtime.http.parse_query(query_string)
Parse a URL query string.
Parameters:
query_string(string): Query string to parse
Returns: Dict of key-value pairs
Request Object
Handlers receive a Request object with these fields:
method(string): HTTP methodpath(string): Request pathbody(string): Request bodyheaders(dict): Request headers (lowercase keys)query(dict): Query parameters
Methods:
json(): Parse body as JSON
Examples
Basic Routes
# setup.py
import scriptling.runtime as runtime
runtime.http.get("/users", "handlers.list_users")
runtime.http.post("/users", "handlers.create_user")
runtime.http.middleware("handlers.auth")Handler with Response
# handlers.py
import scriptling.runtime as runtime
def list_users(request):
users = runtime.kv.get("users", default=[])
return runtime.http.json(200, {"users": users})
def create_user(request):
data = request.json()
users = runtime.kv.get("users", default=[])
users.append(data)
runtime.kv.set("users", users)
return runtime.http.json(201, {"user": data})Middleware
import scriptling.runtime as runtime
def auth(request):
if "authorization" not in request.headers:
return runtime.http.json(401, {"error": "Unauthorized"})
return None # Continue to handlerNotes
- Routes are registered during setup script execution
- Response helpers return dicts compatible with the server
- Middleware can short-circuit requests by returning a response