Unable to make https POST request

Hello,

I’m not able to make an HTTPS POST request with Zig 0.15.1 using the following code.
When I run this code, I get the error: HttpConnectionClosing.

I found this issue: std.http.Client blocks forever with larger payloads on HTTPS requests · Issue #25015 · ziglang/zig · GitHub
However, that report is about large payloads, while I’m experiencing the same issue even with small payloads (using the same code).

Do you have any idea what might be causing this?

const std = @import("std");

const Client = std.http.Client;

pub fn main() !void {
    const alloc = std.heap.page_allocator;

    var allocating = std.Io.Writer.Allocating.init(alloc);
    defer allocating.deinit();

    const opts: Client.FetchOptions = .{
        .method = .POST,
        .location = .{ .url = "https://httpbin.org/post" },
        .payload = "Hello World",
        .response_writer = &allocating.writer,
    };

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

    _ = try client.fetch(opts);

    std.debug.print("{s}\n", .{allocating.written()});
}

I ran into this too. It seemed to occur with anything over ~800 bytes. I couldn’t solve it, my assistant couldn’t solve it. Then I saw that same issue in the ziglang repo and threw my hands up.

I ended up vendoring in libcurl/mbedtls stripped down to the essentials and have had good success doing that. I would much prefer to use zig stdlib though. I’m also using zig 0.15.1.

Cheers

2 Likes