Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkXmlW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xml;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkXmlW xmlResp;

    success = FALSE;

    // 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

    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"soapenv:Envelope");
    CkXmlW_AddAttribute(xml,L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance");
    CkXmlW_AddAttribute(xml,L"xmlns:xsd",L"http://www.w3.org/2001/XMLSchema");
    CkXmlW_AddAttribute(xml,L"xmlns:soapenv",L"http://schemas.xmlsoap.org/soap/envelope/");
    CkXmlW_AddAttribute(xml,L"xmlns:urn",L"urn:server");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Header",L"");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento",TRUE,L"soapenv:encodingStyle",L"http://schemas.xmlsoap.org/soap/encoding/");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento",TRUE,L"xsi:type",L"urn:emitir_dte");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|encabezado",TRUE,L"xsi:type",L"urn:encabezado");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|encabezado|tipo_dte",TRUE,L"xsi:type",L"xsd:string");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|encabezado|tipo_dte",L"39");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|encabezado|fecha_emision",TRUE,L"xsi:type",L"xsd:date");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|encabezado|fecha_emision",L"2019-08-03");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|encabezado|condiciones_pago",TRUE,L"xsi:type",L"xsd:string");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|encabezado|condiciones_pago",L"0");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|encabezado|receptor_email",TRUE,L"xsi:type",L"xsd:string");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|encabezado|receptor_email",L"example@gmail.com");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles",TRUE,L"xsi:type",L"urn:detalles");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles|detalle",TRUE,L"xsi:type",L"urn:detalle");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|cantidad",TRUE,L"xsi:type",L"xsd:int");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|cantidad",L"3");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|glosa",TRUE,L"xsi:type",L"xsd:string");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|glosa",L"Recarga Agua Purificada");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|monto_unitario",TRUE,L"xsi:type",L"xsd:decimal");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|monto_unitario",L"1932.773109");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|exento_afecto",TRUE,L"xsi:type",L"xsd:boolean");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|exento_afecto",L"1");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|totales",TRUE,L"xsi:type",L"urn:totales");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|totales|total_exento",TRUE,L"xsi:type",L"xsd:int");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|totales|total_exento",L"0");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|totales|total_afecto",TRUE,L"xsi:type",L"xsd:int");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|totales|total_afecto",L"5798");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|totales|total_iva",TRUE,L"xsi:type",L"xsd:int");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|totales|total_iva",L"1102");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|urn:emitirDocumento|documento|totales|total_final",TRUE,L"xsi:type",L"xsd:Int");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|urn:emitirDocumento|documento|totales|total_final",L"6900");

    http = CkHttpW_Create();

    CkHttpW_putBasicAuth(http,TRUE);
    CkHttpW_putLogin(http,L"9.999.999-9/mylogin");
    CkHttpW_putPassword(http,L"mypassword");

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

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpStr(http,L"POST",L"https://conexion.facto.cl/documento.php",CkXmlW_getXml(xml),L"utf-8",L"text/xml",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkXmlW_Dispose(xml);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    // A status code = 200 indicates success.
    wprintf(L"Response status code = %d\n",CkHttpResponseW_getStatusCode(resp));

    xmlResp = CkXmlW_Create();
    CkXmlW_LoadXml(xmlResp,CkHttpResponseW_bodyStr(resp));
    wprintf(L"Response body text:\n");
    wprintf(L"%s\n",CkXmlW_getXml(xmlResp));

    // 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>


    CkXmlW_Dispose(xml);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkXmlW_Dispose(xmlResp);

    }