### Basic GET Request in Zig Source: https://github.com/bitlytwiser/zinger/blob/main/README.md A fundamental example of making a GET request using Zinger. It initializes the client, sets up headers, performs the request, and includes basic error checking for the response. ```zig const allocator = std.heap.page_allocator; var z = zinger.Zinger.init(allocator, null); defer z.deinit(); var headers = [_]std.http.Header{}; const resp = try z.get("", null, &headers); if (resp.err()) |err_data| { std.debug.print("{s}", .{err_data.phrase}); } if (resp.err() != null) { try resp.printErr(); } ``` -------------------------------- ### Install Zinger with Zon Source: https://github.com/bitlytwiser/zinger/blob/main/README.md Demonstrates how to fetch and save the Zinger library using the Zon package manager in Zig. ```sh zig fetch --save https://github.com/BitlyTwiser/zinger/archive/refs/tags/v0.1.1.tar.gz ``` -------------------------------- ### GET Request with JSON Response Parsing in Zig Source: https://github.com/bitlytwiser/zinger/blob/main/README.md Demonstrates how to make a GET request and then parse the JSON response body into a specified Zig struct. It includes error handling and printing the parsed data. ```zig const allocator = std.heap.page_allocator; var z = zinger.Zinger.init(allocator, 1024 * 2 * 2); defer z.deinit(); var headers = [_]std.http.Header{}; const resp = try z.get("", null, &headers); if (resp.err()) |err_data| { std.debug.print("{s}", .{err_data.phrase}); } if (resp.err() != null) { try resp.printErr(); } // Serialize the JSON data from the body using the json(anytype) method. // Pass any struct type here to marshal the body into the struct. // Obviously, ensure that the struct attributes match the returned JSON data from the endpoint const json_resp = try resp.json(test_resp_type); std.debug.print("{any}", .{json_resp}); ``` -------------------------------- ### Initialize Zinger with Custom Max Append Size Source: https://github.com/bitlytwiser/zinger/blob/main/README.md Provides examples of initializing the Zinger client with or without a custom maximum body append size. The default is 2MB, but can be overridden to prevent over-allocation. ```zig var z = zinger.Zinger.init(allocator, null); // OR // The numerical value here should be carefully considered to avoid over allocation. var z = zinger.Zinger.init(allocator, 1024 * 10); ``` -------------------------------- ### Import Zinger in Zig Source: https://github.com/bitlytwiser/zinger/blob/main/README.md Illustrates the basic import statement for using the Zinger library within a Zig source file. ```zig const zinger = @import("zinger").Zinger; ``` -------------------------------- ### Execute PUT Request with JSON Body using Zinger Source: https://github.com/bitlytwiser/zinger/blob/main/README.md Illustrates how to perform a PUT request with a JSON body and custom headers using the Zinger library. This includes setting up the Zinger client, preparing and serializing the data, defining headers, executing the PUT request, and processing the response, such as deserializing JSON content. ```zig fn put(allocator: std.mem.Allocator) !void { // Create Zinger instance for POST var z = zinger.Zinger.init(allocator, null); const test_data = struct { example_string: []const u8, }{ .example_string = "testing", }; const json_body = try std.json.stringifyAlloc(allocator, test_data, .{}); defer allocator.free(json_body); var headers = [_]std.http.Header{.{ .name = "content-type", .value = "application/json" }}; var resp = try z.put("", json_body, &headers); if (resp.err() |err_data|) { std.debug.print("{s}", .{err_data.phrase}); return; } // Serialize the JSON data from the body using the json(anytype) method. const json_resp = try resp.json(test_resp_type); std.debug.print("{any}", .{json_resp}); } ``` -------------------------------- ### POST Request with JSON Body and Response Parsing in Zig Source: https://github.com/bitlytwiser/zinger/blob/main/README.md Illustrates how to perform a POST request with a JSON payload and then parse the JSON response. This includes defining a response struct, stringifying the request body, setting the content-type header, and handling the response. ```zig const test_resp_type = struct { test_data: []const u8, }; fn post(allocator: std.mem.Allocator) !void { // Create Zinger instance for POST var z = zinger.Zinger.init(allocator, null); const test_data = struct { example_string: []const u8, }{ .example_string = "testing", }; const json_body = std.json.stringifyAlloc(allocator, test_data, .{}); defer allocator.free(json_body); var headers = [_]std.http.Header{.{ .name = "content-type", .value = "application/json" }}; const resp = try z.get("", test_data, &headers); if (resp.err()) |err_data| { std.debug.print("{s}", .{err_data.phrase}); } if (resp.err() != null) { try resp.printErr(); } // Serialize the JSON data from the body using the json(anytype) method using the custom struct above const resp_data = struct { example_string: []const u8, }{}; const json_resp = try resp.json(resp_data); std.debug.print("{any}", .{json_resp}); } ``` -------------------------------- ### Add Zinger to Zig build.zig Source: https://github.com/bitlytwiser/zinger/blob/main/README.md Shows how to add Zinger as a dependency in your Zig project's build.zig file and import its module. ```zig const zinger = b.dependency("zinger", .{}); exe.root_module.addImport("zinger", zinger.module("zinger")); ``` -------------------------------- ### Execute DELETE Request with JSON Body using Zinger Source: https://github.com/bitlytwiser/zinger/blob/main/README.md Demonstrates how to send a DELETE request with a JSON payload and headers using the Zinger library. It shows the initialization of the Zinger client, serialization of data to JSON, setting the 'content-type' header, executing the request, and handling the response, including deserializing a JSON response. ```zig fn delete(allocator: std.mem.Allocator) !void { // Create Zinger instance for POST // The value for the max_override here is just symbolic for reference, // this would generally be null unless you *need* to override this value var z = zinger.Zinger.init(allocator, 2048 * 10); const test_data = struct { example_string: []const u8, }{ .example_string = "testing", }; const json_body = try std.json.stringifyAlloc(allocator, test_data, .{}); defer allocator.free(json_body); var headers = [_]std.http.Header{.{ .name = "content-type", .value = "application/json" }}; var resp = try z.delete("", json_body, &headers); if (resp.err() != null) { try resp.printErr(); return; } // Serialize the JSON data from the body using the json(anytype) method. const json_resp = try resp.json(test_resp_type); std.debug.print("{any}", .{json_resp}); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.