Last active
March 11, 2023 18:04
-
-
Save musaprg/b73aa918816989db7c15b7b75d214ca4 to your computer and use it in GitHub Desktop.
http client test with zig on master channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const heap = std.heap; | |
const http = std.http; | |
const Uri = std.Uri; | |
const debug = std.debug; | |
pub fn main() anyerror!void { | |
var arena = heap.ArenaAllocator.init(std.heap.page_allocator); | |
defer arena.deinit(); | |
const allocator = arena.allocator(); | |
var client: http.Client = .{.allocator = allocator}; | |
defer client.deinit(); | |
const uri = try Uri.parse("http://example.com"); | |
debug.print("parsed schema: {s}\n", .{uri.scheme}); | |
debug.print("parsed path: {s}\n", .{uri.path}); | |
debug.print("parsed host: {s}\n", .{uri.host orelse ""}); | |
var req = try client.request(uri, .{}, .{}); | |
var buf_reader = std.io.bufferedReader(req.reader()); | |
var in_stream = buf_reader.reader(); | |
var buf: [1024]u8 = undefined; | |
while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| { | |
debug.print("{s}\n", .{line}); | |
} | |
debug.print("Method: {s}\n", .{@tagName(req.headers.method)}); | |
debug.print("Status code: {s}\n", .{@tagName(req.response.headers.status)}); | |
debug.print("Version: {s}\n", .{@tagName(req.response.headers.version)}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://example.com
http://example.com/