Skip to content

HTTP

Core HTTP/1.1 protocol types following RFC 7231. Provides method enumeration, status codes with reason phrases, header management, and MIME type detection.

Import

zig
const api = @import("api");
const http = api.http;
const Method = api.Method;
const StatusCode = api.StatusCode;

Method

HTTP request methods supported by api.zig:

MethodConstantDescription
GET.GETRetrieve resources
POST.POSTCreate resources
PUT.PUTReplace resources
DELETE.DELETERemove resources
PATCH.PATCHPartial update
HEAD.HEADHeaders only
OPTIONS.OPTIONSCORS preflight
TRACE.TRACEDebug/diagnostic
CONNECT.CONNECTTunnel connection
zig
pub const Method = enum {
    GET,
    POST,
    PUT,
    DELETE,
    PATCH,
    HEAD,
    OPTIONS,
    TRACE,
    CONNECT,
};

Methods

fromString

zig
pub fn fromString(str: []const u8) ?Method

Parses a string into a Method.

toString

zig
pub fn toString(self: Method) []const u8

Returns the string representation.

StatusCode

HTTP status codes organized by category:

Informational (1xx)

CodeConstantPhrase
100.@"continue"Continue
101.switching_protocolsSwitching Protocols

Success (2xx)

CodeConstantPhrase
200.okOK
201.createdCreated
202.acceptedAccepted
204.no_contentNo Content

Redirection (3xx)

CodeConstantPhrase
301.moved_permanentlyMoved Permanently
302.foundFound
304.not_modifiedNot Modified
307.temporary_redirectTemporary Redirect
308.permanent_redirectPermanent Redirect

Client Error (4xx)

CodeConstantPhrase
400.bad_requestBad Request
401.unauthorizedUnauthorized
403.forbiddenForbidden
404.not_foundNot Found
405.method_not_allowedMethod Not Allowed
409.conflictConflict
422.unprocessable_entityUnprocessable Entity
429.too_many_requestsToo Many Requests

Server Error (5xx)

CodeConstantPhrase
500.internal_server_errorInternal Server Error
502.bad_gatewayBad Gateway
503.service_unavailableService Unavailable
504.gateway_timeoutGateway Timeout

Methods

toInt

zig
pub fn toInt(self: StatusCode) u16

Returns the numeric value.

phrase

zig
pub fn phrase(self: StatusCode) []const u8

Returns the reason phrase.

isSuccess

zig
pub fn isSuccess(self: StatusCode) bool

Returns true for 2xx codes.

isRedirect

zig
pub fn isRedirect(self: StatusCode) bool

Returns true for 3xx codes.

isClientError

zig
pub fn isClientError(self: StatusCode) bool

Returns true for 4xx codes.

isServerError

zig
pub fn isServerError(self: StatusCode) bool

Returns true for 5xx codes.

ContentTypes

Common MIME types.

zig
const ContentTypes = http.Headers.ContentTypes;

ContentTypes.json        // "application/json"
ContentTypes.html        // "text/html; charset=utf-8"
ContentTypes.plain       // "text/plain; charset=utf-8"
ContentTypes.xml         // "application/xml"
ContentTypes.form        // "application/x-www-form-urlencoded"
ContentTypes.css         // "text/css"
ContentTypes.javascript  // "application/javascript"
ContentTypes.png         // "image/png"
ContentTypes.jpeg        // "image/jpeg"

getMimeType

zig
pub fn getMimeType(path_str: []const u8) []const u8

Determines MIME type from file extension.

zig
http.getMimeType("style.css")   // "text/css"
http.getMimeType("data.json")   // "application/json"
http.getMimeType("image.png")   // "image/png"

Example

zig
fn handler(ctx: *api.Context) api.Response {
    const method = ctx.method();

    if (method == .POST) {
        return api.Response.jsonRaw("{}")
            .setStatus(.created);
    }

    return api.Response.text(method.toString());
}

Released under the MIT License.