HTTP core types for Zig inspired by Rust http.
http is available on astrolabe.pm via gyro
gyro add ducdetronquito/http
Create an HTTP request
constRequest=@import("http").Request;
conststd=@import("std");
varrequest=tryRequest.builder(std.testing.allocator)
.get("https://ziglang.org/")
.header("GOTTA-GO", "FAST")
.body("");
deferrequest.deinit();Create an HTTP response
constResponse=@import("http").Request;
constStatusCode=@import("http").StatusCode;
conststd=@import("std");
varresponse=tryResponse.builder(std.testing.allocator)
.status(.Ok)
.header("GOTTA-GO", "FAST")
.body("");
deferresponse.deinit();// The default constructorfninit(allocator: *Allocator) Headers// Add a header name and valuefnappend(self: *Headers, name: []constu8, value: []constu8) !void// Retrieve the first matching headerfnget(self: Headers, name: []constu8) ?Header// Retrieve a list of matching headersfnlist(self: Headers, name: []constu8) ![]Header// Retrieve the number of headersfnlen(self: Headers) usize// Retrieve all headersfnitems(self: Headers) []HeaderHeader issues are tracked here: #2
constRequest=struct {
method: Method,
uri: Uri,
version: Version,
headers: Headers,
body: []constu8,
};// The default constructor to start building a requestfnbuilder(allocator: *Allocator) RequestBuilder// Release the memory allocated by the headersfndeinit(self: *Request) void// The default constructordefault(allocator: *Allocator) RequestBuilder// Set the request's payload.// This function returns the final request objet or a potential error// collected during the build stepsfnbody(self: *RequestBuilder, value: []constu8) RequestError!Request// Shortcut to define a CONNECT request to the provided URIfnconnect(self: *RequestBuilder, _uri: []constu8) *RequestBuilder// Shortcut to define a DELETE request to the provided URIfndelete(self: *RequestBuilder, _uri: []constu8) *RequestBuilder// Shortcut to define a GET request to the provided URIfnget(self: *RequestBuilder, _uri: []constu8) *RequestBuilder// Shortcut to define an HEAD request to the provided URIfnhead(self: *RequestBuilder, _uri: []constu8) *RequestBuilder// Set a request header name and valuefnheader(self: *RequestBuilder, name: []constu8, value: []constu8) *RequestBuilder// Set the request's methodfnmethod(self: *RequestBuilder, value: Method) *RequestBuilder// Shortcut to define an OPTIONS request to the provided URIfnoptions(self: *RequestBuilder, _uri: []constu8) *RequestBuilder// Shortcut to define a PATCH request to the provided URIfnpatch(self: *RequestBuilder, _uri: []constu8) *RequestBuilder// Shortcut to define a POST request to the provided URIfnpost(self: *RequestBuilder, _uri: []constu8) *RequestBuilder// Shortcut to define a PUT request to the provided URIfnput(self: *RequestBuilder, _uri: []constu8) *RequestBuilder// Shortcut to define a TRACE request to the provided URIfntrace(self: *RequestBuilder, _uri: []constu8) *RequestBuilder// Set the request's URIfnuri(self: *RequestBuilder, value: []constu8) *RequestBuilder// Set the request's protocol versionfnversion(self: *RequestBuilder, value: Version) *RequestBuilderconstResponse=struct {
status: StatusCode,
version: Version,
headers: Headers,
body: []constu8,
};// The default constructor to start building a responsefnbuilder(allocator: *Allocator) ResponseBuilder// The default constructordefault(allocator: *Allocator) ResponseBuilder// Set the response's payload.// This function returns the final response objet or a potential error// collected during the build stepsfnbody(self: *ResponseBuilder, value: []constu8) ResponseError!Response// Set a response header name and valuefnheader(self: *ResponseBuilder, name: []constu8, value: []constu8) *ResponseBuilder// Set the response's status codefnstatus(self: *ResponseBuilder, value: StatusCode) *ResponseBuilder// Set the response's protocol versionfnversion(self: *ResponseBuilder, value: Version) *ResponseBuilder- Connect
- Custom
- Delete
- Get
- Head
- Options
- Patch
- Post
- Put
- Trace
A lot; the list is available on MDN.
- Http09
- Http10
- Http11
- Http2
- Http3
- OutOfMemory
- Invalid
- OutOfMemory
- UriRequired
- URI errors
- OutOfMemory
http is released under the BSD Zero clause license. 🎉🍻
The URI parser is a fork of Vexu's zuri under the MIT License.