Zig
Zig
POST JSON Without Waiting for the Response
See more REST Examples
This example demonstrates sending a POST w/ content-type "application/json" where the body of the POST contains a JSON document. The POST is sent, but we don't wait for the response.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to the server using TLS
const b_auto_reconnect = false;
rest.connect("example.com", 443, true, b_auto_reconnect) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// Create some sample JSON to be sent in the body of the request.
const sb_json = try chilkat.StringBuilder.init();
defer sb_json.deinit();
sb_json.append("{\"create\": [{\"name\": \"Woo Single #1\",\"type\": \"simple\",\"regular_price\": \"21.99\"}]}") catch {};
// Add the Content-Type HTTP request header.
rest.addHeader("Content-Type", "application/json") catch {};
// Just send the request. Don't read the response.
// The request is sent to https://example.com/something?arg1=xyz&arg2=abc
const some_path = "/something?arg1=xyz&arg2=abc";
rest.sendReqSb("POST", some_path, sb_json) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// OK, the request was sent.
// Close the connection.
const max_wait_ms = 50;
rest.disconnect(max_wait_ms) catch {};
std.debug.print("JSON POST Sent.\n", .{});
}