#[SOLVED] is there a way to put a `Request` body into a dynamic array

1 messages · Page 1 of 1 (latest)

dark tulip
#

i don't know the lenght of the request body at comptime so currently i just use a pretty big buffer for Request.readAll

#

is there a way to replace the buffer (just a pretty long []u8) with a dynamic array of some sort?

neat echo
#

isn't there a req.readAllAlloc() method you can use?

#

if not, you might look though test/standalone/http.zig for examples

dark tulip
#

gonna post a snippet here brb

#
fn fetchBody(
    url: []const u8,
    buf: *std.ArrayList(u8),
    bufMaxSize: usize,
    allocator: std.mem.Allocator,
) !void {
    var headers = http.Headers{ .allocator = allocator };
    defer headers.deinit();

    var client = http.Client{ .allocator = allocator };
    defer client.deinit();

    const uri = try std.Uri.parse(url);
    var req = try client.request(.GET, uri, headers, .{});
    defer req.deinit();

    try req.start();
    try req.wait();

    const reader = req.reader();
    try reader.readAllArrayList(buf, bufMaxSize);
}```
#

something like this works fine