Printing the body of an http response

zig version: 0.11.0-dev.3348+3faf376b0

Hello, I am working on a project in Zig in which I need to make an http GET request to a server which returns a body of JSON. I’ve been trying to accomplish this with the new http client in the standard library. Though with the lack of available examples (in the official documentation or otherwise), I am having a hard time reasoning with the http client source code to figure out how i can accomplish this task.

const std = @import("std");
const allocator = std.heap.page_allocator;
const uri = std.Uri.parse("https://aur.archlinux.org/rpc/v5/search/zig-git") catch unreachable;

pub fn main() !void {
    var client: std.http.Client = .{ .allocator = allocator };
    defer client.deinit();

    var req = try client.request(.GET, uri, .{ .allocator = allocator }, .{});

    try req.start();
    try req.wait();

    std.debug.print("{any}\n", .{req.response});
}

this returns a lot of information, however, it does not return the body of the response. As an example of what result I am looking for:

sh $ curl https://aur.archlinux.org/rpc/v5/search/zig-git

{"resultcount":4,"results":[
{"Description":"Neovim configuration for Zig","FirstSubmitted":1615705010,"ID":876874,"LastModified":1615705010,"Maintainer":"jukan","Name":"nvim-zig-git","NumVotes":0,"OutOfDate":null,"PackageBase":"nvim-zig-git","PackageBaseID":164314,"Popularity":0,"URL":"https://github.com/ziglang/zig.vim","URLPath":"/cgit/aur.git/snapshot/nvim-zig-git.tar.gz","Version":"1:r129.ead2193-1"},
{"Description":"Zig grammar for tree-sitter","FirstSubmitted":1623456216,"ID":920057,"LastModified":1623456216,"Maintainer":null,"Name":"tree-sitter-zig-git","NumVotes":0,"OutOfDate":null,"PackageBase":"tree-sitter-zig-git","PackageBaseID":167884,"Popularity":0,"URL":"https://github.com/grayjack/tree-sitter-zig","URLPath":"/cgit/aur.git/snapshot/tree-sitter-zig-git.tar.gz","Version":"r50.41bbc7f-1"},
{"Description":"Vim configuration for Zig","FirstSubmitted":1559947178,"ID":1118384,"LastModified":1658979318,"Maintainer":"NBonaparte","Name":"vim-zig-git","NumVotes":2,"OutOfDate":null,"PackageBase":"vim-zig-git","PackageBaseID":142602,"Popularity":0.000003,"URL":"https://github.com/ziglang/zig.vim","URLPath":"/cgit/aur.git/snapshot/vim-zig-git.tar.gz","Version":"1:r155.3538ae9-1"},
{"Description":"a programming language prioritizing robustness, optimality, and clarity","FirstSubmitted":1470764596,"ID":1224368,"LastModified":1678508008,"Maintainer":"leopoldek","Name":"zig-git","NumVotes":11,"OutOfDate":null,"PackageBase":"zig-git","PackageBaseID":114485,"Popularity":0.065176,"URL":"https://ziglang.org","URLPath":"/cgit/aur.git/snapshot/zig-git.tar.gz","Version":"0.10.0.r1928.g3169f0529b-1"}],"type":"search","version":5}%

The response body of this URL returns JSON as expected.

I’m not necessarily looking for someone to “do my homework for me” (although I would definitely accept thorough responses), I am more so looking for a push in the right direction.

Thank you, and if this question is too vague, please let me know so I can revise it.

Check the example at :

I have attempted to use this example before posting this question, when printing the ‘body’ from this example, the output of this does not print anything at all. The example also does not show anything about printing the body, just allocating space for it in memory.

Isn’t this (from that example) what you are looking for?

const body = req.reader().readAllAlloc(allocator);
defer allocator.free(body);

No, readAllAlloc() requires 2 arguments, and even when given a second argument (from the example), this does not return the expected result of JSON when printed.

below is the result of using the example from the article

{ 123, 34, 114, 101, 115, 117, 108, 116, 99, 111, 117, 110, 116, 34, 58, 52, 44, 34, 114, 101, 115, 117, 108, 116, 115, 34, 58, 91, 123, 34, 68, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 34, 58, 34, 78, 101, 111, 118, 105, 109, 32, 99, 111, 110, 102, 105, 103, 117, 114, 97, 116, 105, 111, 110, 32, 102, 111, 114, 32, 90, 105, 103, 34, 44, 34, 70, 105, 114, 115, 116, 83, 117, 98, 109, 105, 116, 116, 101, 100, 34, 58, 49, 54, 49, 53, 55, 48, 53, 48, 49, 48, 44, 34, 73, 68, 34, 58, 56, 55, 54, 56, 55, 52, 44, 34, 76, 97, 115, 116, 77, 111, 100, 105, 102, 105, 101, 100, 34, 58, 49, 54, 49, 53, 55, 48, 53, 48, 49, 48, 44, 34, 77, 97, 105, 110, 116, 97, 105, 110, 101, 114, 34, 58, 34, 106, 117, 107, 97, 110, 34, 44, 34, 78, 97, 109, 101, 34, 58, 34, 110, 118, 105, 109, 45, 122, 105, 103, 45, 103, 105, 116, 34, 44, 34, 78, 117, 109, 86, 111, 116, 101, 115, 34, 58, 48, 44, 34, 79, 117, 116, 79, 102, 68, 97, 116, 101, 34, 58, 110, 117, 108, 108, 44, 34, 80, 97, 99, 107, 97, 103, 101, 66, 97, 115, 101, 34, 58, 34, 110, 118, 105, 109, 45, 122, 105, 103, 45, 103, 105, 116, 34, 44, 34, 80, 97, 99, 107, 97, 103, 101, 66, 97, 115, 101, 73, 68, 34, 58, 49, 54, 52, 51, 49, 52, 44, 34, 80, 111, 112, 117, 108, 97, 114, 105, 116, 121, 34, 58, 48, 44, 34, 85, 82, 76, 34, 58, 34, 104, 116, 116, 112, 115, 58, 47, 47, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 122, 105, 103, 108, 97, 110, 103, 47, 122, 105, 103, 46, 118, 105, 109, 34, 44, 34, 85, 82, 76, 80, 97, 116, 104, 34, 58, 34, 47, 99, 103, 105, 116, 47, 97, 117, 114, 46, 103, 105, 116, 47, 115, 110, 97, 112, 115, 104, 111, 116, 47, 110, 118, 105, 109, 45, 122, 105, 103, 45, 103, 105, 116, 46, 116, 97, 114, 46, 103, 122, 34, 44, 34, 86, 101, 114, 115, 105, 111, 110, 34, 58, 34, 49, 58, 114, 49, 50, 57, 46, 101, 97, 100, 50, 49, 57, 51, 45, 49, 34, 125, 44, 123, 34, 68, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 34, 58, 34, 90, 105, 103, 32, 103, 114, 97, 109, 109, 97, 114, 32, 102, 111, 114, 32, 116, 114, 101, 101, 45, 115, 105, 116, 116, 101, 114, 34, 44, 34, 70, 105, 114, 115, 116, 83, 117, 98, 109, 105, 116, 116, 101, 100, 34, 58, 49, 54, 50, 51, 52, 53, 54, 50, 49, 54, 44, 34, 73, 68, 34, 58, 57, 50, 48, 48, 53, 55, 44, 34, 76, 97, 115, 116, 77, 111, 100, 105, 102, 105, 101, 100, 34, 58, 49, 54, 50, 51, 52, 53, 54, 50, 49, 54, 44, 34, 77, 97, 105, 110, 116, 97, 105, 110, 101, 114, 34, 58, 110, 117, 108, 108, 44, 34, 78, 97, 109, 101, 34, 58, 34, 116, 114, 101, 101, 45, 115, 105, 116, 116, 101, 114, 45, 122, 105, 103, 45, 103, 105, 116, 34, 44, 34, 78, 117, 109, 86, 111, 116, 101, 115, 34, 58, 48, 44, 34, 79, 117, 116, 79, 102, 68, 97, 116, 101, 34, 58, 110, 117, 108, 108, 44, 34, 80, 97, 99, 107, 97, 103, 101, 66, 97, 115, 101, 34, 58, 34, 116, 114, 101, 101, 45, 115, 105, 116, 116, 101, 114, 45, 122, 105, 103, 45, 103, 105, 116, 34, 44, 34, 80, 97, 99, 107, 97, 103, 101, 66, 97, 115, 101, 73, 68, 34, 58, 49, 54, 55, 56, 56, 52, 44, 34, 80, 111, 112, 117, 108, 97, 114, 105, 116, 121, 34, 58, 48, 44, 34, 85, 82, 76, 34, 58, 34, 104, 116, 116, 112, 115, 58, 47, 47, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 103, 114, 97, 121, 106, 97, 99, 107, 47, 116, 114, 101, 101, 45, 115, 105, 116, 116, 101, 114, 45, 122, 105, 103, 34, 44, 34, 85, 82, 76, 80, 97, 116, 104, 34, 58, 34, 47, 99, 103, 105, 116, 47, 97, 117, 114, 46, 103, 105, 116, 47, 115, 110, 97, 112, 115, 104, 111, 116, 47, 116, 114, 101, 101, 45, 115, 105, 116, 116, 101, 114, 45, 122, 105, 103, 45, 103, 105, 116, 46, 116, 97, 114, 46, 103, 122, 34, 44, 34, 86, 101, 114, 115, 105, 111, 110, 34, 58, 34, 114, 53, 48, 46, 52, 49, 98, 98, 99, 55, 102, 45, 49, 34, 125, 44, 123, 34, 68, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 34, 58, 34, 86, 105, 109, 32, 99, 111, 110, 102, 105, 103, 117, 114, 97, 116, 105, 111, 110, 32, 102, 111, 114, 32, 90, 105, 103, 34, 44, 34, 70, 105, 114, 115, 116, 83, 117, 98, 109, 105, 116, 116, 101, 100, 34, 58, 49, 53, 53, 57, 57, 52, 55, 49, 55, 56, 44, 34, 73, 68, 34, 58, 49, 49, 49, 56, 51, 56, 52, 44, 34, 76, 97, 115, 116, 77, 111, 100, 105, 102, 105, 101, 100, 34, 58, 49, 54, 53, 56, 57, 55, 57, 51, 49, 56, 44, 34, 77, 97, 105, 110, 116, 97, 105, 110, 101, 114, 34, 58, 34, 78, 66, 111, 110, 97, 112, 97, 114, 116, 101, 34, 44, 34, 78, 97, 109, 101, 34, 58, 34, 118, 105, 109, 45, 122, 105, 103, 45, 103, 105, 116, 34, 44, 34, 78, 117, 109, 86, 111, 116, 101, 115, 34, 58, 50, 44, 34, 79, 117, 116, 79, 102, 68, 97, 116, 101, 34, 58, 110, 117, 108, 108, 44, 34, 80, 97, 99, 107, 97, 103, 101, 66, 97, 115, 101, 34, 58, 34, 118, 105, 109, 45, 122, 105, 103, 45, 103, 105, 116, 34, 44, 34, 80, 97, 99, 107, 97, 103, 101, 66, 97, 115, 101, 73, 68, 34, 58, 49, 52, 50, 54, 48, 50, 44, 34, 80, 111, 112, 117, 108, 97, 114, 105, 116, 121, 34, 58, 48, 46, 48, 48, 48, 48, 48, 51, 44, 34, 85, 82, 76, 34, 58, 34, 104, 116, 116, 112, 115, 58, 47, 47, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 122, 105, 103, 108, 97, 110, 103, 47, 122, 105, 103, 46, 118, 105, 109, 34, 44, 34, 85, 82, 76, 80, 97, 116, 104, 34, 58, 34, 47, 99, 103, 105, 116, 47, 97, 117, 114, 46, 103, 105, 116, 47, 115, 110, 97, 112, 115, 104, 111, 116, 47, 118, 105, 109, 45, 122, 105, 103, 45, 103, 105, 116, 46, 116, 97, 114, 46, 103, 122, 34, 44, 34, 86, 101, 114, 115, 105, 111, 110, 34, 58, 34, 49, 58, 114, 49, 53, 53, 46, 51, 53, 51, 56, 97, 101, 57, 45, 49, 34, 125, 44, 123, 34, 68, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 34, 58, 34, 97, 32, 112, 114, 111, 103, 114, 97, 109, 109, 105, 110, 103, 32, 108, 97, 110, 103, 117, 97, 103, 101, 32, 112, 114, 105, 111, 114, 105, 116, 105, 122, 105, 110, 103, 32, 114, 111, 98, 117, 115, 116, 110, 101, 115, 115, 44, 32, 111, 112, 116, 105, 109, 97, 108, 105, 116, 121, 44, 32, 97, 110, 100, 32, 99, 108, 97, 114, 105, 116, 121, 34, 44, 34, 70, 105, 114, 115, 116, 83, 117, 98, 109, 105, 116, 116, 101, 100, 34, 58, 49, 52, 55, 48, 55, 54, 52, 53, 57, 54, 44, 34, 73, 68, 34, 58, 49, 50, 50, 52, 51, 54, 56, 44, 34, 76, 97, 115, 116, 77, 111, 100, 105, 102, 105, 101, 100, 34, 58, 49, 54, 55, 56, 53, 48, 56, 48, 48, 56, 44, 34, 77, 97, 105, 110, 116, 97, 105, 110, 101, 114, 34, 58, 34, 108, 101, 111, 112, 111, 108, 100, 101, 107, 34, 44, 34, 78, 97, 109, 101, 34, 58, 34, 122, 105, 103, 45, 103, 105, 116, 34, 44, 34, 78, 117, 109, 86, 111, 116, 101, 115, 34, 58, 49, 49, 44, 34, 79, 117, 116, 79, 102, 68, 97, 116, 101, 34, 58, 110, 117, 108, 108, 44, 34, 80, 97, 99, 107, 97, 103, 101, 66, 97, 115, 101, 34, 58, 34, 122, 105, 103, 45, 103, 105, 116, 34, 44, 34, 80, 97, 99, 107, 97, 103, 101, 66, 97, 115, 101, 73, 68, 34, 58, 49, 49, 52, 52, 56, 53, 44, 34, 80, 111, 112, 117, 108, 97, 114, 105, 116, 121, 34, 58, 48, 46, 48, 54, 53, 49, 55, 54, 44, 34, 85, 82, 76, 34, 58, 34, 104, 116, 116, 112, 115, 58, 47, 47, 122, 105, 103, 108, 97, 110, 103, 46, 111, 114, 103, 34, 44, 34, 85, 82, 76, 80, 97, 116, 104, 34, 58, 34, 47, 99, 103, 105, 116, 47, 97, 117, 114, 46, 103, 105, 116, 47, 115, 110, 97, 112, 115, 104, 111, 116, 47, 122, 105, 103, 45, 103, 105, 116, 46, 116, 97, 114, 46, 103, 122, 34, 44, 34, 86, 101, 114, 115, 105, 111, 110, 34, 58, 34, 48, 46, 49, 48, 46, 48, 46, 114, 49, 57, 50, 56, 46, 103, 51, 49, 54, 57, 102, 48, 53, 50, 57, 98, 45, 49, 34, 125, 93, 44, 34, 116, 121, 112, 101, 34, 58, 34, 115, 101, 97, 114, 99, 104, 34, 44, 34, 118, 101, 114, 115, 105, 111, 110, 34, 58, 53, 125 }

whereas the expected result:

[user@host] $ curl https://aur.archlinux.org/rpc/v5/search/zig-git
{"resultcount":4,"results":[{"Description":"Neovim configuration for Zig","FirstSubmitted":1615705010,"ID":876874,"LastModified":1615705010,"Maintainer":"jukan","Name":"nvim-zig-git","NumVotes":0,"OutOfDate":null,"PackageBase":"nvim-zig-git","PackageBaseID":164314,"Popularity":0,"URL":"https://github.com/ziglang/zig.vim","URLPath":"/cgit/aur.git/snapshot/nvim-zig-git.tar.gz","Version":"1:r129.ead2193-1"},{"Description":"Zig grammar for tree-sitter","FirstSubmitted":1623456216,"ID":920057,"LastModified":1623456216,"Maintainer":null,"Name":"tree-sitter-zig-git","NumVotes":0,"OutOfDate":null,"PackageBase":"tree-sitter-zig-git","PackageBaseID":167884,"Popularity":0,"URL":"https://github.com/grayjack/tree-sitter-zig","URLPath":"/cgit/aur.git/snapshot/tree-sitter-zig-git.tar.gz","Version":"r50.41bbc7f-1"},{"Description":"Vim configuration for Zig","FirstSubmitted":1559947178,"ID":1118384,"LastModified":1658979318,"Maintainer":"NBonaparte","Name":"vim-zig-git","NumVotes":2,"OutOfDate":null,"PackageBase":"vim-zig-git","PackageBaseID":142602,"Popularity":0.000003,"URL":"https://github.com/ziglang/zig.vim","URLPath":"/cgit/aur.git/snapshot/vim-zig-git.tar.gz","Version":"1:r155.3538ae9-1"},{"Description":"a programming language prioritizing robustness, optimality, and clarity","FirstSubmitted":1470764596,"ID":1224368,"LastModified":1678508008,"Maintainer":"leopoldek","Name":"zig-git","NumVotes":11,"OutOfDate":null,"PackageBase":"zig-git","PackageBaseID":114485,"Popularity":0.065176,"URL":"https://ziglang.org","URLPath":"/cgit/aur.git/snapshot/zig-git.tar.gz","Version":"0.10.0.r1928.g3169f0529b-1"}],"type":"search","version":5}%

You need to use the {s} format specifier to have a slice print as a string.

std.debug.print("{s}\n", .{req.response});
1 Like

this was enough to point me in the right direction, thank you very much.

marked as solution, but for clarification of anyone reading this with the same issue, you would use the {s} format specifier on the body, not the req.response

1 Like