Sample code for 30+ languages & platforms
Zig

Send a REST Request with a Text Body

See more REST Examples

Demonstrates Rest.FullRequestString, a convenience method that sends a complete request whose text body (such as JSON, XML, or plain text) is supplied directly, then reads and returns the response body as text.

Background. The full-request convenience methods perform the entire request/response exchange in a single call. FullRequestString is well suited to typical JSON or XML API calls where the body fits comfortably in a string.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const rest = try chilkat.Rest.init();
    defer rest.deinit();
    const b_tls = true;
    const b_auto_reconnect = true;
    rest.connect("example.com", 443, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Send a complete request whose text body is supplied directly, such as JSON.  The response body is
    // read as text and returned.  The 1st argument is the HTTP verb, the 2nd is the URI path, and the
    // 3rd is the request body.
    rest.addHeader("Content-Type", "application/json") catch {};

    const json_body = "{ \"name\": \"example\", \"active\": true }";
    const response_text = rest.fullRequestString(alloc, "POST", "/api/items", json_body) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{response_text});
}