Last active
October 18, 2025 23:31
-
-
Save Zorgatone/968ce86711aecea984a2c4a9771eed5f to your computer and use it in GitHub Desktop.
Zig 0.15.1 http-client GET example
This file contains hidden or 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 builtin = @import("builtin"); | |
| const std = @import("std"); | |
| // This version won't read/print headers, just the response | |
| pub fn main() !void { | |
| var writer_buffer: [8 * 1024]u8 = undefined; | |
| var redirect_buffer: [8 * 1024]u8 = undefined; | |
| var writer = std.fs.File.stdout().writer(&writer_buffer); | |
| var debug_allocator: std.heap.DebugAllocator(.{}) = .init; | |
| defer switch (builtin.mode) { | |
| .Debug => std.debug.assert(debug_allocator.deinit() == .ok), | |
| .ReleaseFast, .ReleaseSmall, .ReleaseSafe => { | |
| // Nothing | |
| }, | |
| }; | |
| const allocator = switch (builtin.mode) { | |
| .Debug => debug_allocator.allocator(), | |
| .ReleaseFast, .ReleaseSmall, .ReleaseSafe => std.heap.smp_allocator, | |
| }; | |
| const uri = try std.Uri.parse("https://postman-echo.com/get"); | |
| var client: std.http.Client = .{ .allocator = allocator }; | |
| defer client.deinit(); | |
| const result = try client.fetch(.{ | |
| .location = .{ .uri = uri }, | |
| .method = .GET, | |
| .redirect_buffer = &redirect_buffer, | |
| .response_writer = &writer.interface, | |
| }); | |
| if (builtin.mode == .Debug) { | |
| std.debug.assert(result.status == .ok); | |
| } | |
| try writer.interface.flush(); | |
| } |
This file contains hidden or 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 builtin = @import("builtin"); | |
| const std = @import("std"); | |
| // Manual http request printing also the response headers (won't work if chunked or compressed) | |
| pub fn main() !void { | |
| var writer_buffer: [8 * 1024]u8 = undefined; | |
| var redirect_buffer: [8 * 1024]u8 = undefined; | |
| var transfer_buffer: [8 * 1024]u8 = undefined; | |
| var reader_buffer: [8 * 1024]u8 = undefined; | |
| var writer = std.fs.File.stdout().writer(&writer_buffer); | |
| var debug_allocator: std.heap.DebugAllocator(.{}) = .init; | |
| defer switch (builtin.mode) { | |
| .Debug => std.debug.assert(debug_allocator.deinit() == .ok), | |
| .ReleaseFast, .ReleaseSmall, .ReleaseSafe => { | |
| // Nothing | |
| }, | |
| }; | |
| const allocator = switch (builtin.mode) { | |
| .Debug => debug_allocator.allocator(), | |
| .ReleaseFast, .ReleaseSmall, .ReleaseSafe => std.heap.smp_allocator, | |
| }; | |
| const uri = try std.Uri.parse("https://postman-echo.com/get"); | |
| var client: std.http.Client = .{ .allocator = allocator }; | |
| defer client.deinit(); | |
| var request = try client.request(.GET, uri, .{}); | |
| defer request.deinit(); | |
| try request.sendBodiless(); | |
| const response = try request.receiveHead(&redirect_buffer); | |
| _ = try writer.interface.write(response.head.bytes); | |
| const content_length = response.head.content_length; | |
| const reader = request.reader.bodyReader(&transfer_buffer, .none, content_length); | |
| var done = false; | |
| var bytes_read: usize = 0; | |
| while (!done) { | |
| const size = try reader.readSliceShort(&reader_buffer); | |
| if (size > 0) { | |
| bytes_read += size; | |
| _ = try writer.interface.write(reader_buffer[0..size]); | |
| } | |
| if (content_length) |c_len| { | |
| if (bytes_read >= c_len) { | |
| done = true; | |
| } | |
| } | |
| if (size < reader_buffer.len) { | |
| done = true; | |
| } | |
| } | |
| try writer.interface.flush(); | |
| } |
Thanks for this. #1 makes sense. #2 your perspective is helping me understand zig better. Your explanation makes sense to me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@definitepotato
well, this was an easy example so you could go with both approaches. Since it's going to use Uri.parse anyway, I did it earlier, in case I want to reuse the same uri for multiple fetch/http requests.
Not sure I understand your question, what do you mean why am I using a file descriptor here? I am using the new
std.Ioin this example (all readers, writers also implement the std.Io interface now, ie.writer.interfaceandreader.interface). Turns out standard output (stdout) is also implemented as a file descriptor (macOs, linux). Your example writer.Allocating + heap.FixedBufferAllocator would make sense if you wanted to write to some array (buffer) in memory you could then read, or chunked.buffered stream/pipe to an actual file on the file system (instead of stdout). Here I'm only printing (streaming/piping) to the console (stdout) to debug the result of the HTTP request (body).Did I answer your question, or did you want to know something else?