Sample code for 30+ languages & platforms
Zig

Simple SOAP Request

See more HTTP Examples

Demonstrates how to send a simple SOAP request.

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

    // --------------------------------------------------------------------------------
    // Also see Chilkat's Online WSDL Code Generator
    // to generate code and SOAP Request and Response XML for each operation in a WSDL.
    // --------------------------------------------------------------------------------

    // Create the following XML to be sent in the SOAP request body.

    // <soapenv:Envelope xmlns:dat="http://www.dataaccess.com/webservicesserver/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    //     <soapenv:Header/>
    //     <soapenv:Body>
    //         <dat:NumberToDollars>
    //             <dat:dNum>99.0</dat:dNum>
    //         </dat:NumberToDollars>
    //     </soapenv:Body>
    // </soapenv:Envelope>

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("soapenv:Envelope");
    xml.addAttribute("xmlns:dat", "http://www.dataaccess.com/webservicesserver/") catch {};
    xml.addAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml.updateChildContent("soapenv:Header", "");
    xml.updateChildContent("soapenv:Body|dat:NumberToDollars|dat:dNum", "99.0");

    // In a SOAP HTTP request, including the XML declaration (<?xml version="1.0" encoding="UTF-8"?>) in the XML body is generally not required.
    xml.setEmitXmlDecl(false);
    const soap_request_body = try xml.getXml(alloc);

    const endpoint = "https://www.dataaccess.com/webservicesserver/numberconversion.wso";
    const soap_action = "";
    //  For SOAP requests, the standard Content-Type is usually set to "text/xml" or "application/soap+xml"
    const content_type = "text/xml";

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

    http.clearHeaders();
    http.setRequestHeader("SOAPAction", soap_action);

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpStr("POST", endpoint, soap_request_body, "utf-8", content_type, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Get the XML response body.
    const response_xml = try chilkat.Xml.init();
    defer response_xml.deinit();
    resp.getBodyXml(response_xml) catch {};

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

    // If the status code does not indicate succcess, then show the response XML,
    // which probably contains error information.
    if (status_code != 200) {
        std.debug.print("{s}\n", .{try response_xml.getXml(alloc)});
        return;
    }

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

    // Parse the successful SOAP response XML.

    // This is a sample of the response XML, but the namespace prefixes will be different.
    // We can parse the result using "*" for the namespace prefixes (see below).

    // <soapenv:Envelope xmlns:dat="http://www.dataaccess.com/webservicesserver/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    //     <soapenv:Header/>
    //     <soapenv:Body>
    //         <dat:NumberToDollarsResponse>
    //             <dat:NumberToDollarsResult>string</dat:NumberToDollarsResult>
    //         </dat:NumberToDollarsResponse>
    //     </soapenv:Body>
    // </soapenv:Envelope>

    const dat_number_to_dollars_result = try response_xml.getChildContent(alloc, "*:Body|*:NumberToDollarsResponse|*:NumberToDollarsResult");
    std.debug.print("result: {s}\n", .{dat_number_to_dollars_result});
}