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

curl with Variable Substitution in an XML Request Body

See more CURL Examples

This example shows how to use variables inside an XML request body using the {{variable_name}} syntax. When the HTTP request’s Content-Type indicates XML, Chilkat automatically applies proper XML entity encoding to each substituted value, ensuring the resulting XML remains valid.

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

    // Variable names are enclosed between {{ and }}

    // curl -X POST https://api.example.com/api/orders \
    //   -H "Content-Type: application/xml; charset=utf-8" \
    //   -H "Accept: application/xml" \
    //   -d '<order>
    //   <customerName>{{customer_name}}</customerName>
    //   <note>{{note}}</note>
    //   <address>{{address}}</address>
    //   <instructions>{{instructions}}</instructions>
    // </order>'

    const sb_curl = try chilkat.StringBuilder.init();
    defer sb_curl.deinit();
    sb_curl.appendLn("curl -X POST https://api.example.com/api/orders \\") catch {};
    sb_curl.appendLn("  -H \"Content-Type: application/xml; charset=utf-8\" \\") catch {};
    sb_curl.appendLn("  -H \"Accept: application/xml\" \\") catch {};
    sb_curl.appendLn("  -d '<order>") catch {};
    sb_curl.appendLn("  <customerName>{{customer_name}}</customerName>") catch {};
    sb_curl.appendLn("  <note>{{note}}</note>") catch {};
    sb_curl.appendLn("  <address>{{address}}</address>") catch {};
    sb_curl.appendLn("  <instructions>{{instructions}}</instructions>") catch {};
    sb_curl.appendLn("</order>'") catch {};

    const curl = try chilkat.HttpCurl.init();
    defer curl.deinit();

    // The values below contain chars that will require XML entity encoding.
    // Chilkat will automatically do the encoding because the Content-Type of this request is "application/xml"
    curl.setVar("customer_name", "John & Sons");
    curl.setVar("note", "He said \"Ship it ASAP!\"");
    curl.setVar("address", "123 <Main> Street");
    curl.setVar("instructions", "Use door #2 & call upon arrival");

    // To demonstrate how the variables are replaced, this example does not execute the curl command.
    // Instead, it generates the raw HTTP request that would be sent if the curl command were run.
    const sb_raw_request = try chilkat.StringBuilder.init();
    defer sb_raw_request.deinit();
    curl.toRawRequest(try sb_curl.getAsString(alloc), sb_raw_request) catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{try sb_raw_request.getAsString(alloc)});

    // The output is shown below.
    // Notice the chars that were XML entity encoded.

    // POST /api/orders HTTP/1.1
    // Accept: application/xml
    // Host: api.example.com
    // Content-Type: application/xml; charset=utf-8
    // Content-Length: 229
    //
    // <order>
    //   <customerName>John &amp; Sons</customerName>
    //   <note>He said &quot;Ship it ASAP!&quot;</note>
    //   <address>123 &lt;Main&gt; Street</address>
    //   <instructions>Use door #2 &amp; call upon arrival</instructions>
    // </order>
}