Sample code for 30+ languages & platforms
Zig

Making SOAP Requests using the REST API

See more REST Examples

There's no reason a SOAP request cannot be sent using the REST API. It's only a matter of adding a SOAP specific header, and providing the SOAP XML request body. The response body is the SOAP response XML.

This example sends a request to ws.cdyne.com to get city weather by zip code.

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

    // Demonstrates how to do a SOAP request using the REST API.

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

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

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

    // This is the request to be sent:

    // POST /WeatherWS/Weather.asmx HTTP/1.1
    // Host: wsf.cdyne.com
    // Content-Type: text/xml; charset=utf-8
    // Content-Length: length
    // SOAPAction: "http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP"
    //
    // <?xml version="1.0" encoding="utf-8"?>
    // <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    //   <soap:Body>
    //     <GetCityWeatherByZIP xmlns="http://ws.cdyne.com/WeatherWS/">
    //       <ZIP>string</ZIP>
    //     </GetCityWeatherByZIP>
    //   </soap:Body>
    // </soap:Envelope>

    // Note: The application does not need to set the Host or Content-Length headers.
    // Chilkat will automatically add these headers.

    // Make the initial connection (without sending a request yet).
    const b_tls = false;
    const port = 80;
    const b_auto_reconnect = true;
    // In this particular case, it is important to connect to "wsf.cdyne.com", not "ws.cdyne.com"...
    rest.connect("wsf.cdyne.com", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Add request headers:
    try rest.addHeader("Content-Type", "text/xml; charset=utf-8");
    try rest.addHeader("SOAPAction", "http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP");

    // Build the SOAP XML request body.
    const soap_xml = try chilkat.Xml.init();
    defer soap_xml.deinit();

    soap_xml.setTag("soap:Envelope");
    try soap_xml.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    try soap_xml.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
    try soap_xml.addAttribute("xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");

    soap_xml.newChild2("soap:Body", "");
    try soap_xml.getChild2(0);
    soap_xml.newChild2("GetCityWeatherByZIP", "");
    try soap_xml.getChild2(0);
    try soap_xml.addAttribute("xmlns", "http://ws.cdyne.com/WeatherWS/");
    soap_xml.newChild2("ZIP", "60187");
    soap_xml.getRoot2();

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

    // Send the SOAP request
    const response_xml = rest.fullRequestString(alloc, "POST", "/WeatherWS/Weather.asmx", try soap_xml.getXml(alloc)) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // When successful, the response status code will equal 200.
    if (rest.getResponseStatusCode() != 200) {
        // Examine the request/response to see what happened.
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body (if any): {s}\n", .{response_xml});
        std.debug.print("---\n", .{});
        std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
        return;
    }

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    try xml.loadXml(response_xml);

    // GetXml will emit XML that is nicely indented for human viewing..
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // A sample response XML is shown below...

    // To get some information, use ChilkatPath.  For example...
    std.debug.print("Temperature: {s}\n", .{try xml.chilkatPath(alloc, "soap:Body|GetCityWeatherByZIPResponse|GetCityWeatherByZIPResult|Temperature|*")});

    std.debug.print("Success.\n", .{});

    // <?xml version="1.0" encoding="utf-8" ?>
    // <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    //     <soap:Body>
    //         <GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/">
    //             <GetCityWeatherByZIPResult>
    //                 <Success>true</Success>
    //                 <ResponseText>City Found</ResponseText>
    //                 <State>IL</State>
    //                 <City>Wheaton</City>
    //                 <WeatherStationCity>West Chicago</WeatherStationCity>
    //                 <WeatherID>10</WeatherID>
    //                 <Description>Mostly Sunny</Description>
    //                 <Temperature>33</Temperature>
    //                 <RelativeHumidity>39</RelativeHumidity>
    //                 <Wind>VRB5</Wind>
    //                 <Pressure>30.33F</Pressure>
    //                 <Visibility />
    //                 <WindChill />
    //                 <Remarks />
    //             </GetCityWeatherByZIPResult>
    //         </GetCityWeatherByZIPResponse>
    //     </soap:Body>
    // </soap:Envelope>
}