#Can you tell me how to send a request with json in the body of an http request in zig?

1 messages · Page 1 of 1 (latest)

neon compass
#

you have to include content-length (and probably content-type) headers, then write the json to the request

alpine pumice
#

hmhm

neon compass
#
        // Add content length header
        var length_str: [32]u8 = undefined;
        const length_str_len = std.fmt.formatIntBuf(length_str[0..], data.len, 10, .lower, .{});
        try headers.append("content-length", length_str[0..length_str_len]);

        try headers.append("content-type", "application/json");

        // Perform the request
        var req = try client.request(.POST, self.endpoint, headers, .{});
        defer req.deinit();

        try req.start();
        _ = try req.write(data);
        try req.wait();
#

where data is the json content

alpine pumice
#
pub fn request(client: *Client, method: http.Method, uri: Uri, headers: http.Headers, options: RequestOptions) RequestError!Request {
```I thought I could set the body here in RequestOptions, is that wrong?
neon compass
alpine pumice
#

hmhm

neon compass
#

start() checks for the existence of a "content-length" header and if it exists allows you to write to the body with write()