Hi , I haven’t used the std HTTP framework that much, but in one of my project I just need to send one HTTP Post Request, to a known address, and I’m not sure what is the simplest most straightforward way of doing so ?
std.http.Client.fetch
fn getWeather(allocator: std.mem.Allocator, city: []const u8, lang: []const u8) ![]const u8 {
var url: std.BoundedArray(u8, 256) = .{};
if (builtin.target.os.tag == .windows) {
try url.writer().print("https://wttr.in/{s}?AFT&lang={s}", .{ city, lang });
} else {
try url.writer().print("https://wttr.in/{s}?AF&lang={s}", .{ city, lang });
}
var body = std.ArrayList(u8).init(allocator);
errdefer body.deinit();
var client: std.http.Client = .{ .allocator = allocator };
defer client.deinit();
_ = try client.fetch(.{
.location = .{ .url = url.constSlice() },
.response_storage = .{ .dynamic = &body },
});
return body.toOwnedSlice();
}
thanks for the answer, but how would you do a POST request (sorry maybe fetch is doing a post, I’m not a web person at all I don’t know much about it). Because I have a little multi threaded pong server, That uses websockets to play pong, and I just need at the end of a game to do a post request of the results to a endpoint. something like https://localhost:8000/pong/results/
so I just need to put some values in the header of a Request, send it, and I don’t really care about much more. The problem is I don’t what’s the correct way of doing that. The localtion with the url writer seems like a very neat and simple way for setting the location, but how would I attach a Request and send it to the address ?
The answer is in the std https://github.com/ziglang/zig/blob/master/lib/std/http/Client.zig#L1689-L1726
Oh thank you didn’t even think about it ahah.