Networking
Wyn provides built-in networking through the Http and Net modules. No external dependencies required.
HTTP Client
// GET request
body = Http.get("https://api.example.com/data")
print(body)
// POST request
response = Http.post("https://api.example.com/users", "{\"name\": \"Wyn\"}")
print(response)HTTP Server
The server model is an accept loop, not a route table. Http.listen binds and returns a server descriptor; Http.accept returns one request at a time.
fn main() -> int {
server = Http.listen(8080)
if server < 0 {
println("could not bind :8080")
return 1
}
println("Server running on :8080")
while true {
req = Http.accept(server)
if req == "" { continue }
fd = req.split_at("|", 3).to_int()
Http.respond(fd, 200, "text/plain", "Hello, World!")
}
return 0
}Always check req == "" and continue. Http.accept returns the empty string for a connection that sends no request - a port scan, a TCP health check, a load-balancer probe or a browser preconnect. Before v1.21.0 that empty string reached .to_int() and killed the process; the runtime now skips such connections, and your handler should too.
For routing, middleware and templates, use the web package, which builds these on top of the accept loop. There is no Http.route in the core runtime.
Working with JSON APIs
fn main() -> int {
body = Http.get("https://api.example.com/users")
doc = Json.parse(body)
name = Json.get(doc, "name")
print("Fetched: ${name}")
return 0
}See Also
SMTP - send emails
Web Framework - routing, middleware, templates
JSON - parsing and generating JSON
Database - SQLite for data persistence