Sample code for 30+ languages & platforms
Zig

SOAP Request to Issue Documents in Facto.cl

See more HTTP Misc Examples

Demonstrates how to make a SOAP HTTP request to issue a document using the facto.cl webservice.

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.

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

    // Here is an example of an HTTP POST Request we'll be sending:

    // POST /documento.php HTTP/1.1
    // Content-Type: text/xml;charset=UTF-8
    // SOAPAction: https://conexion.facto.cl/documento.php/emitirDocumento
    //
    // <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:server">
    //    <soapenv:Header/>
    //    <soapenv:Body>
    //       <urn:emitirDocumento soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    //          <documento xsi:type="urn:emitir_dte">
    //             <encabezado xsi:type="urn:encabezado">
    //                <tipo_dte xsi:type="xsd:string">39</tipo_dte>
    //                <fecha_emision xsi:type="xsd:date">2019-08-03</fecha_emision>
    //                <condiciones_pago xsi:type="xsd:string">0</condiciones_pago>
    //                <receptor_email xsi:type="xsd:string">example@gmail.com</receptor_email>
    //             </encabezado>
    //             <detalles xsi:type="urn:detalles">
    //                <detalle xsi:type="urn:detalle">
    //                   <cantidad xsi:type="xsd:int">3</cantidad>
    //                   <glosa xsi:type="xsd:string">Recarga Agua Purificada</glosa>
    //                   <monto_unitario xsi:type="xsd:decimal">1932.773109</monto_unitario>
    //                   <exento_afecto xsi:type="xsd:boolean">1</exento_afecto>
    //                </detalle>
    //             </detalles>
    //             <totales xsi:type="urn:totales">
    //                <total_exento xsi:type="xsd:int">0</total_exento>
    //                <total_afecto xsi:type="xsd:int">5798</total_afecto>
    //                <total_iva xsi:type="xsd:int">1102</total_iva>
    //                <total_final xsi:type="xsd:Int">6900</total_final>
    //             </totales>
    //          </documento>
    //       </urn:emitirDocumento>
    //    </soapenv:Body>
    // </soapenv:Envelope>

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

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("soapenv:Envelope");
    xml.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") catch {};
    xml.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema") catch {};
    xml.addAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml.addAttribute("xmlns:urn", "urn:server") catch {};
    xml.updateChildContent("soapenv:Header", "");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento", true, "soapenv:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/") catch {};
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento", true, "xsi:type", "urn:emitir_dte") catch {};
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|encabezado", true, "xsi:type", "urn:encabezado") catch {};
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|encabezado|tipo_dte", true, "xsi:type", "xsd:string") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|encabezado|tipo_dte", "39");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|encabezado|fecha_emision", true, "xsi:type", "xsd:date") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|encabezado|fecha_emision", "2019-08-03");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|encabezado|condiciones_pago", true, "xsi:type", "xsd:string") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|encabezado|condiciones_pago", "0");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|encabezado|receptor_email", true, "xsi:type", "xsd:string") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|encabezado|receptor_email", "example@gmail.com");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|detalles", true, "xsi:type", "urn:detalles") catch {};
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|detalles|detalle", true, "xsi:type", "urn:detalle") catch {};
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|cantidad", true, "xsi:type", "xsd:int") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|cantidad", "3");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|glosa", true, "xsi:type", "xsd:string") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|glosa", "Recarga Agua Purificada");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|monto_unitario", true, "xsi:type", "xsd:decimal") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|monto_unitario", "1932.773109");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|exento_afecto", true, "xsi:type", "xsd:boolean") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|exento_afecto", "1");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|totales", true, "xsi:type", "urn:totales") catch {};
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|totales|total_exento", true, "xsi:type", "xsd:int") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|totales|total_exento", "0");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|totales|total_afecto", true, "xsi:type", "xsd:int") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|totales|total_afecto", "5798");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|totales|total_iva", true, "xsi:type", "xsd:int") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|totales|total_iva", "1102");
    xml.updateAttrAt("soapenv:Body|urn:emitirDocumento|documento|totales|total_final", true, "xsi:type", "xsd:Int") catch {};
    xml.updateChildContent("soapenv:Body|urn:emitirDocumento|documento|totales|total_final", "6900");

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

    http.setBasicAuth(true);
    http.setLogin("9.999.999-9/mylogin");
    http.setPassword("mypassword");

    http.setRequestHeader("SoapAction", "https://conexion.facto.cl/documento.php/emitirDocumento");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpStr("POST", "https://conexion.facto.cl/documento.php", try xml.getXml(alloc), "utf-8", "text/xml", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // A status code = 200 indicates success.
    std.debug.print("Response status code = {d}\n", .{resp.getStatusCode()});

    const xml_resp = try chilkat.Xml.init();
    defer xml_resp.deinit();
    xml_resp.loadXml(try resp.getBodyStr(alloc)) catch {};
    std.debug.print("Response body text:\n", .{});
    std.debug.print("{s}\n", .{try xml_resp.getXml(alloc)});

    // The response will look like this:

    // <?xml version="1.0" encoding="UTF-8"?>
    // <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    //     <SOAP-ENV:Body>
    //         <ns1:emitirDocumentoResponse xmlns:ns1="urn:server">
    //             <return xsi:type="xsd:complexType">
    //                 <resultado>
    //                     <status>0</status>
    //                     <mensaje_error/>
    //                 </resultado>
    //                 <encabezado>
    //                     <tipo_dte>39</tipo_dte>
    //                     <folio>109999</folio>
    //                     <fecha_emision>2019-08-03</fecha_emision>
    //                     <condiciones_pago>0</condiciones_pago>
    //                     <receptor_email>example@gmail.com</receptor_email>
    //                 </encabezado>
    //                 <detalles>
    //                     <cantidad>3</cantidad>
    //                     <glosa>Recarga Agua Purificada</glosa>
    //                     <monto_unitario>1932.773109</monto_unitario>
    //                     <exento_afecto>1</exento_afecto>
    //                 </detalles>
    //                 <totales>
    //                     <total_exento>0</total_exento>
    //                     <total_afecto>5798</total_afecto>
    //                     <total_iva>1102</total_iva>
    //                     <total_final>6900</total_final>
    //                 </totales>
    //                 <enlaces>
    //                     <dte_xml>https://temporal.facto.cl/2019/08/999993022aa9e0733174e87086ade7d4.xml</dte_xml>
    //                     <dte_pdf>https://temporal.facto.cl/2019/08/99999f52cf80c8ffc1fc22338dae022.pdf</dte_pdf>
    //                     <dte_timbre>https://temporal.facto.cl/2019/08/999999856c08b3d24a83bf29add5364a.png</dte_timbre>
    //                 </enlaces>
    //             </return>
    //         </ns1:emitirDocumentoResponse>
    //     </SOAP-ENV:Body>
    // </SOAP-ENV:Envelope>
}