Skip to content

Commit

Permalink
Update Zig HTTP example to Zig 0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Jun 7, 2024
1 parent a74f39e commit 095c403
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions binaries/zig/http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ pub fn main() !void {
// we can `catch unreachable` here because we can guarantee that this is a valid url.
const uri = std.Uri.parse("https://example.com/") catch unreachable;

// these are the headers we'll be sending to the server
var headers = std.http.Headers{ .allocator = alloc };
defer headers.deinit();

try headers.append("accept", "*/*"); // tell the server we'll accept anything
var read_buffer: [8000]u8 = undefined;

// make the connection and set up the request
var req = try client.open(.GET, uri, headers, .{});
var req = try client.open(.GET, uri, .{
.server_header_buffer = &read_buffer,
.extra_headers = &.{
.{ .name = "cookie", .value = "RISCV=awesome" },
},
});
defer req.deinit();

// send the request and headers to the server.
try req.send(.{});
try req.send();
try req.wait();

// read the content-type header from the server, or default to text/plain
const content_type = req.response.headers.getFirstValue("content-type") orelse "text/plain";
_ = content_type;

// read the entire response body
const body = req.reader().readAllAlloc(alloc, 1024 * 1024) catch unreachable;
defer alloc.free(body);
Expand Down

0 comments on commit 095c403

Please sign in to comment.