An HTTP client inspired by httpx and ureq.
requestz is available on astrolabe.pm via gyro
gyro add ducdetronquito/requestz
Send a GET request
const client = @import("requestz.zig").Client;
var client = try Client.init(std.testing.allocator);
defer client.deinit();
var response = try client.get("http://httpbin.org/get", .{});
defer response.deinit();
Send a request with headers
const Headers = @import("http").Headers;
var headers = Headers.init(std.testing.allocator);
defer headers.deinit();
try headers.append("Gotta-go", "Fast!");
var response = try client.get("http://httpbin.org/get", .{ .headers = headers.items() });
defer response.deinit();
Send a request with compile-time headers
var headers = .{
.{"Gotta-go", "Fast!"}
};
var response = try client.get("http://httpbin.org/get", .{ .headers = headers });
defer response.deinit();
Send binary data along with a POST request
var response = try client.post("http://httpbin.org/post", .{ .content = "Gotta go fast!" });
defer response.deinit();
var tree = try response.json();
defer tree.deinit();
Stream a response
var response = try client.stream(.Get, "http://httpbin.org/", .{});
defer response.deinit();
while(true) {
var buffer: [4096]u8 = undefined;
var bytesRead = try response.read(&buffer);
if (bytesRead == 0) {
break;
}
std.debug.print("{}", .{buffer[0..bytesRead]});
}
Other standard HTTP method shortcuts:
client.connect
client.delete
client.head
client.options
client.patch
client.put
client.trace
requestz is released under the BSD Zero clause license. 🎉🍻