About

Style Guidelines
  • snake_case

    • variables.

    • namespace.

  • camelCase

    • functions.

  • PascalCase

    • types.

Examples

HTTP Requests Example
const std = @import("std");
const http = @import("zig-http/http");

pub fn main() !void {
    const allocator = std.heap.page_allocator;

    // Create the HTTP server
    var server = try http.Server.init(allocator, "0.0.0.0", 8080, handler);

    std.debug.print("Server running at http://localhost:8080\n", .{});

    // Start the server
    try server.listen();
}

// Function that handles HTTP requests
fn handler(req: *http.Request, res: *http.Response) !void {
    // Process only GET requests
    if (req.method != .Get) {
        res.status = .MethodNotAllowed;
        try res.send("Method not allowed");
        return;
    }

    // Get the "name" parameter from the URL
    const name = req.getQueryParam("name") orelse "world";

    // Create the response message
    const message = try std.fmt.allocPrint(std.heap.page_allocator, "Hello, {}!", .{name});

    // Set status and response body
    res.status = .Ok;
    try res.send(message);

    // Free the memory allocated for the message
    std.heap.page_allocator.free(message);
}