ucoreHttp
Complete HTTP client and server functionality for building REST APIs and web applications.
API Reference
| Function | Returns | Description |
|---|---|---|
get(url) | string | HTTP GET request |
post(url, body) | string | HTTP POST request with body |
put(url, body) | string | HTTP PUT request with body |
patch(url, body) | string | HTTP PATCH request with body |
delete(url) | string | HTTP DELETE request |
json(data) | string | Serialize Map/Array to JSON string |
route(method, path, handler) | bool | Register route handler (string name) |
listen(port, [handler]) | bool | Start HTTP server on port |
use(middleware) | bool | Register global middleware |
static(path, dir) | bool | Serve static files from directory |
HTTP Client
Make HTTP requests to external APIs and services.
get(url)
Performs an HTTP GET request and returns the response body as a string.
// Simple GET request
var response = ucoreHttp.get("https://api.example.com/users");
print(response);
// Parse JSON response
var data = ucoreJson.parse(response);
print(data["name"]);
post(url, body)
Sends an HTTP POST request with the provided body string.
var payload = map();
payload["username"] = "alice";
payload["email"] = "alice@example.com";
var body = ucoreHttp.json(payload);
var response = ucoreHttp.post("https://api.example.com/users", body);
print(response);
put(url, body) / patch(url, body)
Update resources with PUT (full replacement) or PATCH (partial update).
// Full update with PUT
var user = map();
user["name"] = "Alice Updated";
user["email"] = "alice.new@example.com";
ucoreHttp.put("https://api.example.com/users/1", ucoreHttp.json(user));
// Partial update with PATCH
var patch = map();
patch["email"] = "newemail@example.com";
ucoreHttp.patch("https://api.example.com/users/1", ucoreHttp.json(patch));
delete(url)
Sends an HTTP DELETE request to remove a resource.
ucoreHttp.delete("https://api.example.com/users/1");
print("User deleted");
json(data)
Serializes a Map or Array to a JSON string. Useful for request bodies.
var data = map();
data["items"] = ["apple", "banana"];
data["count"] = 2;
var jsonStr = ucoreHttp.json(data);
print(jsonStr); // {"items":["apple","banana"],"count":2}
HTTP Server
Build web servers and REST APIs with routing support.
Request Object
Handler functions receive a request Map containing:
| Key | Type | Description |
|---|---|---|
method | string | HTTP method (GET, POST, PUT, DELETE, PATCH) |
path | string | Request path (/api/users) |
body | string | Request body (for POST/PUT/PATCH) |
query | Map | Query parameters (?id=1 → {"id":"1"}) |
route(method, path, handlerName)
Registers a route with a handler function. Handler name is passed as a string.
function handleUsers(req) {
if (req["method"] == "GET") {
return "[{\"id\":1,\"name\":\"Alice\"}]";
}
return "{\"error\":\"Method not allowed\"}";
}
function handleUserById(req) {
// Extract ID from path manually or query params
return "{\"id\":1,\"name\":\"Alice\"}";
}
// Register routes
ucoreHttp.route("GET", "/api/users", "handleUsers");
ucoreHttp.route("POST", "/api/users", "handleUsers");
ucoreHttp.route("GET", "/api/users/1", "handleUserById");
listen(port, [handlerName])
Starts the HTTP server. Without a handler, uses registered routes. With a handler, all requests go to that single handler.
// Mode 1: Use registered routes
ucoreHttp.route("GET", "/", "handleIndex");
ucoreHttp.route("GET", "/api", "handleApi");
ucoreHttp.listen(8080); // Uses routes
// Mode 2: Single handler for all requests
function handleAll(req) {
print("Request: " + req["method"] + " " + req["path"]);
return "OK";
}
ucoreHttp.listen(8080, "handleAll");
use(middlewareName)
Registers global middleware that runs before all route handlers.
function logMiddleware(req) {
print("[" + ucoreTimer.now() + "] " + req["method"] + " " + req["path"]);
return nil; // Continue to route handler
}
ucoreHttp.use("logMiddleware");
static(urlPath, directory)
Serves static files from a local directory at the specified URL path.
// Serve files from ./public at /static/*
ucoreHttp.static("/static", "./public");
// Access: http://localhost:8080/static/style.css
// Serves: ./public/style.css
ucoreHttp.listen(8080);
Complete REST API Example
// ==================== Handlers ====================
var users = [];
function getUsers(req) {
return ucoreHttp.json(users);
}
function createUser(req) {
var newUser = ucoreJson.parse(req["body"]);
newUser["id"] = length(users) + 1;
push(users, newUser);
return ucoreHttp.json(newUser);
}
function healthCheck(req) {
var res = map();
res["status"] = "ok";
res["uptime"] = ucoreTimer.now();
return ucoreHttp.json(res);
}
// ==================== Routes ====================
ucoreHttp.route("GET", "/api/users", "getUsers");
ucoreHttp.route("POST", "/api/users", "createUser");
ucoreHttp.route("GET", "/health", "healthCheck");
// ==================== Start Server ====================
print("Server running on http://localhost:8080");
ucoreHttp.listen(8080);
Run Example
./bin/unnarize examples/corelib/http/server.unna