#How to use Client.fetch on Zig `0.15.0-dev.1564+2761cc8be` to get the body?

1 messages · Page 1 of 1 (latest)

past stratus
#
   var body: std.io.Writer = undefined;

   const fetchResult = try client.fetch(.{
      .method = .GET,
      .location = .{ .uri = uri },
      .response_writer = &body,
      .keep_alive = true,
      .headers = .{
         .user_agent = .{ .override = "test-fetch" },
         .content_type = .{ .override = "text/html" },
      },
   });

on 0.14.1 it's pretty straight forward since it only requires ArrayList to store the body, but on latest dev version I don't know how to initialized it and store the body since it require Io.Writer. Thanks.

stable bridge
# past stratus ```zig var body: std.io.Writer = undefined; const fetchResult = try clien...
var body_writer: std.Io.Writer.Allocating = .init(allocator);
defer body_writer.deinit();

const fetchResult = try client.fetch(.{
    .method = .GET,
    .location = .{ .uri = uri },
    .response_writer = &body_writer.writer,
    .keep_alive = true,
    .headers = .{
        .user_agent = .{ .override = "test-fetch" },
        .content_type = .{ .override = "text/html" },
    },
});

const body = try body_writer.toOwnedSlice();
past stratus
# stable bridge ```ts var body_writer: std.Io.Writer.Allocating = .init(allocator); defer body_w...

I tried that, but it gives me error : TlsInitializationFailed. is the fetch function broken on this version? or I did wrong when I initialized it? here is my short code during testing it:

const std = @import("std");
const Client = std.http.Client;

pub fn main() !void {
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer std.debug.assert(gpa.deinit() == .ok);
    const allocator = gpa.allocator();

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

    const url = "https://google.com";
    const uri = try std.Uri.parse(url);

    var body_writter: std.io.Writer.Allocating = .init(allocator);
    defer body_writter.deinit();

    _ = try client.fetch(.{
        .method = .GET,
        .location = .{ .uri = uri },
        .keep_alive = true,
        .headers = .{ 
            .user_agent = .{ .override = "test-fetch" }, 
            .content_type = .{ .override = "html/text" },
        },
        .response_writer = &body_writter.writer,
    });

    const body = try body_writter.toOwnedSlice();

    std.debug.print("{any}", .{body});
}
stable bridge
#

hmmm works for me

#

what's the error trace?

past stratus
# stable bridge what's the error trace?
error: TlsInitializationFailed
C:\Zig\zig-x86_64-windows-0.15.0-dev.1564+2761cc8be\lib\std\net.zig:1993:29: 0x7ff7343d2598 in readVec (zzzz_zcu.obj)
                if (n == 0) return error.EndOfStream;
                            ^
C:\Zig\zig-x86_64-windows-0.15.0-dev.1564+2761cc8be\lib\std\Io\Reader.zig:1071:36: 0x7ff7344241b6 in fillUnbuffered (zzzz_zcu.obj)
    while (r.end < r.seek + n) _ = try r.vtable.readVec(r, &bufs);
                                   ^
C:\Zig\zig-x86_64-windows-0.15.0-dev.1564+2761cc8be\lib\std\Io\Reader.zig:1057:5: 0x7ff7343c38dd in fill (zzzz_zcu.obj)
    return fillUnbuffered(r, n);
    ^
C:\Zig\zig-x86_64-windows-0.15.0-dev.1564+2761cc8be\lib\std\Io\Reader.zig:496:5: 0x7ff7343ab361 in peek (zzzz_zcu.obj)
    try r.fill(n);
    ^
C:\Zig\zig-x86_64-windows-0.15.0-dev.1564+2761cc8be\lib\std\Io\Reader.zig:544:20: 0x7ff7343ab660 in take (zzzz_zcu.obj)
    const result = try r.peek(n);
                   ^
C:\Zig\zig-x86_64-windows-0.15.0-dev.1564+2761cc8be\lib\std\crypto\tls\Client.zig:343:34: 0x7ff734374b9c in init (zzzz_zcu.obj)
            error.EndOfStream => return error.TlsConnectionTruncated,
                                 ^
C:\Zig\zig-x86_64-windows-0.15.0-dev.1564+2761cc8be\lib\std\http\Client.zig:338:25: 0x7ff7342e02b3 in create (zzzz_zcu.obj)
                ) catch return error.TlsInitializationFailed,
                        ^
C:\Zig\zig-x86_64-windows-0.15.0-dev.1564+2761cc8be\lib\std\http\Client.zig:1421:24: 0x7ff7342d03af in connectTcpOptions (zzzz_zcu.obj)
            const tc = try Connection.Tls.create(client, proxied_host, proxied_port, stream);
                       ^