Http post request with payload is sent bodiless

I’m working with the code below. I’ve been digging into the Client.zig file and looking at how the fetch call is made and haven’t been able to figure out why this is happening. When I look at the server side where the API the call is made, I can see the API call being made without the payload (only \r\n in the body), though I can validate that up to the point fetch() is called, there is a valid payload in the FetchOptions.

const std = @import("std");
const print = std.debug.print;
const assert = std.debug.assert;

pub fn main() !void {
    const url: []const u8 = "https://example.com";
    const payload =
        \\{
        \\  "name": "Alice",
        \\  "handle": "Alice",
        \\  "age": "23",
        \\  "id": "12345"
        \\}
    ;

    const allocator = std.heap.page_allocator;

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

    const payload_len = try std.fmt.allocPrint(allocator, "{}", .{payload.len});
    const header = &[_]std.http.Header{
        .{ .name = "Referer", .value = url },
        .{ .name = "Content-Type", .value = "application/json" },
        .{ .name = "Content-Length", .value = payload_len },
        .{ .name = "X-Api-Key", .value = "abc123" },
    };

    const headers = try allocator.alloc(std.http.Header, header.len);
    defer allocator.free(headers);

    for (header, 0..) |h, i| {
        headers[i] = h;
    }

    var res_body: std.Io.Writer.Allocating = .init(allocator);
    defer res_body.deinit();

    const fetch_res = try client.fetch(.{
        .location = .{ .url = url },
        .method = .POST,
        .payload = payload,
        .response_writer = &res_body.writer,
        .extra_headers = headers,
    });

    if (fetch_res.status != .ok) {
        print("error fetching: {s}\nstatus: {}\n\n", .{ url, fetch_res.status });
    }

    try res_body.writer.flush();

    const res_body_owned = try res_body.toOwnedSlice();
    print("{s}\n", .{res_body_owned});
}

so it’s hanging in fetch?

Possibly relevant:

Thanks for this. It doesn’t hang. I’m able to get a response for GET and POST requests. The problem is when I look on the API server side the body is empty. In my code I’m sending a body. I’m not sure why it sends bodiless, it should only do that if the payload is null.

in that case you should be sharing the server code, or a cut down example

I guess I should investigate a bit more. I didn’t write the server side it’s an application that I installed. Here’s an SDK I was working on in Zig for this same application GitHub - definitepotato/espocrmz: An API client for EspoCRM in Zig..

Using zig 0.14 this SDK works and I can say with certainty the server side works, and to the best of my ability I can determine using the new std.Io interface and zig 0.15 the payload isn’t being sent when calling fetch even though the payload is defined and has a value in my code.

I will try without calling fetch and doing it manually step-by-step to see how that works out. This could all be my error.