Sample code for 30+ languages & platforms
Zig

Debug REST HTTP Request

See more REST Examples

Demonstrates how to generate the HTTP Request (with all headers intact) without actually sending the request.

Note: This example requires Chilkat v9.5.0.77 or later.

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.

    // This example will connect to the web server, but does not actually send a request.
    // When in DebugMode, the request is composed in memory and can be retrieved by calling
    // GetLastDebugRequest.

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect Code...
    // URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    rest.connect("test-api.service.hmrc.gov.uk", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("ConnectFailReason: {d}\n", .{rest.getConnectFailReason()});
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Build the request body...
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("periodKey", "A001") catch {};
    json.updateNumber("vatDueSales", "105.50") catch {};
    json.updateNumber("vatDueAcquisitions", "-100.45") catch {};
    json.updateNumber("totalVatDue", "5.05") catch {};
    json.updateNumber("vatReclaimedCurrPeriod", "105.15") catch {};
    json.updateNumber("netVatDue", "100.10") catch {};
    json.updateInt("totalValueSalesExVAT", 300) catch {};
    json.updateInt("totalValuePurchasesExVAT", 300) catch {};
    json.updateInt("totalValueGoodsSuppliedExVAT", 3000) catch {};
    json.updateInt("totalAcquisitionsExVAT", 3000) catch {};
    json.updateBool("finalised", true) catch {};

    // Add Headers...
    rest.addHeader("Accept", "application/vnd.hmrc.1.0+json") catch {};
    rest.addHeader("Authorization", "Bearer HMRC_ACCESS_TOKEN") catch {};
    rest.addHeader("Content-Type", "application/json") catch {};

    const sb_request_body = try chilkat.StringBuilder.init();
    defer sb_request_body.deinit();
    json.emitSb(sb_request_body) catch {};

    // Set DebugMode so that no request is actually sent.
    rest.setDebugMode(true);

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    rest.fullRequestSb("POST", "/organisations/vat/MY_HMRC_VRN/returns", sb_request_body, sb_response_body) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Get the exact contents of what would've been sent.
    // This includes the HTTP start line, the HTTP request headers, and the request body.
    // Given that it's possible for the request body to contain binary data,
    // the GetLastDebugRequest fetches into a BinData object.
    // In this case, however, our request body contained JSON, so we can
    // examine it as a string..
    const bd_request = try chilkat.BinData.init();
    defer bd_request.deinit();
    try rest.getLastDebugRequest(bd_request);

    std.debug.print("----\n", .{});
    std.debug.print("{s}\n", .{try bd_request.getString(alloc, "utf-8")});
    std.debug.print("----\n", .{});

    // The output for the above case:

    // POST /organisations/vat/MY_HMRC_VRN/returns HTTP/1.1
    // Accept: application/vnd.hmrc.1.0+json
    // Host: test-api.service.hmrc.gov.uk
    // Authorization: Bearer HMRC_ACCESS_TOKEN
    // Content-Type: application/json
    // Content-Length: 281
    //
    // {"periodKey":"A001","vatDueSales":105.50, ... ,"finalised":true}
    //
    //
}