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

TicketBAI -- Send HTTP POST

See more TicketBAI Examples

Demonstrates how to send a TicketBAI POST and get the response.

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();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setSslClientCertPfx("your.pfx", "pfx_password") catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Get the XML we wish to send in the body of the request.
    const sb_xml = try chilkat.StringBuilder.init();
    defer sb_xml.deinit();
    sb_xml.loadFile("qa_data/payload.xml", "utf-8") catch {
        std.debug.print("Failed to load XML that is to be the HTTP request body\n", .{});
        return;
    };

    // Build the following JSON

    // {
    //     "con": "LROE",
    //     "apa": "1.1",
    //     "inte": {
    //         "nif": "número de identificación fiscal",
    //         "nrs": "nombre o Razón social",
    //         "ap1": "primer apellido",
    //         "ap2": "segundo apellido"
    //     },
    //     "drs": {
    //     "mode": "140",
    //     "ejer": "ejercicio"
    //     }
    // }

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("con", "LROE") catch {};
    json.updateString("apa", "1.1") catch {};
    json.updateString("inte.nif", "número de identificación fiscal") catch {};
    json.updateString("inte.nrs", "nombre o Razón social") catch {};
    json.updateString("inte.ap1", "primer apellido") catch {};
    json.updateString("inte.ap2", "segundo apellido") catch {};
    json.updateString("drs.mode", "140") catch {};
    json.updateString("drs.ejer", "ejercicio") catch {};

    // Add required headers...
    http.setRequestHeader("eus-bizkaia-n3-version", "1.0");
    http.setRequestHeader("eus-bizkaia-n3-content-type", "application/xml");
    http.setRequestHeader("eus-bizkaia-n3-data", try json.emit(alloc));

    const url = "https://pruesarrerak.bizkaia.eus/N3B4000M/aurkezpena";
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.setUncommonOptions("SendGzipped");
    http.httpSb("POST", url, sb_xml, "utf-8", "application/octet-stream", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    http.clearHeaders();

    std.debug.print("response status code: {d}\n", .{resp.getStatusCode()});

    // Examine the response (it is already decompressed)
    std.debug.print("response body:\n", .{});
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
}