Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Example: Http.HttpSReq method

Demonstrates how to call the HttpSReq method.

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 http = try chilkat.Http.init();
    defer http.deinit();
    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();

    // Specify the details of the HTTP request.
    req.setHttpVerb("POST");
    req.setContentType("application/json");
    req.setPath("/echo_request_body.asp");

    const request_body = "{\"greeting\":\"Hello World\"}";
    req.loadBodyFromString(request_body, "utf-8") catch {};

    // Send a POST to https://chilkatsoft.com/echo_request_body.asp with a JSON request body.
    http.httpSReq("chilkatsoft.com", 443, true, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response Status Code: {d}\n", .{resp.getStatusCode()});

    // Examine the request we sent.
    // The LastHeader property stores the HTTP request header of the most recent request sent,
    // and in this instance, the response body mirrors the request body.
    std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});

    // Output:

    // POST /echo_request_body.asp HTTP/1.1
    // Host: chilkatsoft.com
    // Content-Type: application/json
    // Content-Length: 26
    //
    // {"greeting":"Hello World"}
}