summaryrefslogtreecommitdiff
path: root/src/EasyHttp.zig
blob: 28228a7dbf467ab11a42ee27930e5a8d1efd2782 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const std = @import("std");

const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const Client = std.http.Client;
const Reader = Request.Reader;
const Request = Client.Request;
const Uri = std.Uri;

pub fn get(parent_allocator: Allocator, uri: Uri) ![]u8 {
    var arena = ArenaAllocator.init(parent_allocator);
    defer arena.deinit();

    const allocator = arena.allocator();

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

    var server_header_buffer: [4096]u8 = undefined;

    var request = try client.open(.GET, uri, .{
        .server_header_buffer = &server_header_buffer,
    });
    defer request.deinit();

    try request.send();
    try request.wait();

    return request.reader().readAllAlloc(parent_allocator, std.math.maxInt(usize));
}