Sample code for 30+ languages & platforms
C

Alabama Motor Fuel Excise Tax E-Filing SOAP XML POST (NewSubmission)

See more HTTP Misc Examples

Demonstrates how to post a NewSubmission to the Alabama MFET (Motor Fuel Excise Tax) e-File.

Chilkat C Downloads

C
#include <C_CkStringBuilder.h>
#include <C_CkXml.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilder sbXmlFileData;
    HCkXml xml;
    HCkHttp http;
    HCkStringBuilder sbXml;
    HCkHttpResponse resp;
    HCkXml xmlResult;
    HCkStringBuilder sbResult;

    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 /WebServices/MFET/ HTTP/1.1
    // Content-Type: text/xml
    // SOAPAction: NewSubmission
    //  
    // <soapenv:Envelope
    //   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    //   xmlns:tem="http://tempuri.org/"
    //   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    //   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    //   <soapenv:Header/>
    //   <soapenv:Body>
    //     <tem:NewSubmission>
    //       <tem:UserName>yourusername</tem:UserName>
    //       <tem:Password>yourpassword</tem:Password>
    //       <tem:File xsi:type="xsd:base64Binary">TRANSMISSION_XML_FILE_CONTENTS_AS_BASE64</tem:File>
    //     </tem:NewSubmission>
    //   </soapenv:Body>
    // </soapenv:Envelope>

    // In the above XML, the "TRANSMISSION_XML_FILE_CONTENTS_AS_BASE64" contains the base64 encoded
    // string of the XML file that contains "<Transmission> ... </Transmission>"
    // First let's load the transmission XML file and get the contents as base64.
    sbXmlFileData = CkStringBuilder_Create();
    success = CkStringBuilder_LoadFile(sbXmlFileData,"qa_data/xml/AL_SR_999802_test.xml","utf-8");
    if (success != TRUE) {
        printf("Failed to load XML file.\n");
        CkStringBuilder_Dispose(sbXmlFileData);
        return;
    }

    CkStringBuilder_Encode(sbXmlFileData,"base64","utf-8");

    // Build the XMl that will be the body of the HTTP request.
    xml = CkXml_Create();
    CkXml_putTag(xml,"soapenv:Envelope");
    CkXml_AddAttribute(xml,"xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
    CkXml_AddAttribute(xml,"xmlns:tem","http://tempuri.org/");
    CkXml_AddAttribute(xml,"xmlns:xsd","http://www.w3.org/2001/XMLSchema");
    CkXml_AddAttribute(xml,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
    CkXml_UpdateChildContent(xml,"soapenv:Header","");
    CkXml_UpdateChildContent(xml,"soapenv:Body|tem:NewSubmission|tem:UserName","yourusername");
    CkXml_UpdateChildContent(xml,"soapenv:Body|tem:NewSubmission|tem:Password","yourpassword");
    CkXml_UpdateAttrAt(xml,"soapenv:Body|tem:NewSubmission|tem:File",TRUE,"xsi:type","xsd:base64Binary");
    CkXml_UpdateChildContent(xml,"soapenv:Body|tem:NewSubmission|tem:File",CkStringBuilder_getAsString(sbXmlFileData));

    http = CkHttp_Create();

    CkHttp_SetRequestHeader(http,"SoapAction","NewSubmission");

    sbXml = CkStringBuilder_Create();
    CkXml_GetXmlSb(xml,sbXml);

    resp = CkHttpResponse_Create();
    success = CkHttp_HttpSb(http,"POST","https://mattest.alabama.gov/WebServices/MFET/",sbXml,"utf-8","text/xml",resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkStringBuilder_Dispose(sbXmlFileData);
        CkXml_Dispose(xml);
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(sbXml);
        CkHttpResponse_Dispose(resp);
        return;
    }

    printf("Response status code = %d\n",CkHttpResponse_getStatusCode(resp));
    printf("Response body text:\n");
    printf("%s\n",CkHttpResponse_bodyStr(resp));

    // The response will look like this:

    // <?xml version="1.0" encoding="utf-8" ?>
    // <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    //     <s:Body>
    //         <NewSubmissionResponse xmlns="http://tempuri.org/">
    //             <NewSubmissionResult i:type="a:base64Binary" xmlns:a="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">BASE64_RESULT</NewSubmissionResult>
    //         </NewSubmissionResponse>
    //     </s:Body>
    // </s:Envelope>

    // We can get the base64 result like this:
    xmlResult = CkXml_Create();
    CkXml_LoadXml(xmlResult,CkHttpResponse_bodyStr(resp));
    sbResult = CkStringBuilder_Create();
    CkStringBuilder_Append(sbResult,CkXml_getChildContent(xmlResult,"s:Body|NewSubmissionResponse|NewSubmissionResult"));
    CkStringBuilder_Decode(sbResult,"base64","utf-8");
    printf("Decoded result:\n");
    printf("%s\n",CkStringBuilder_getAsString(sbResult));

    // A sample of a decoded error response:

    // <?xml version="1.0" encoding="utf-8"?>
    // <!--ADOR Acknowledgement 1-->
    // <Transmission xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.irs.gov/efile">
    //   <Jurisdiction>ALABAMA</Jurisdiction>
    //   <TransmissionId>2018-09-1019:14R000000001</TransmissionId>
    //   <Timestamp>2018-09-10T19:14:12Z</Timestamp>
    //   <Transmitter>
    //     <ETIN>00000</ETIN>
    //   </Transmitter>
    //   <ProcessType>P</ProcessType>
    //   <AgentIdentifier>ACK</AgentIdentifier>
    //   <AcknowledgementID>0</AcknowledgementID>
    //   <AcknowledgementTimeStamp>2018-09-11T21:16:34-05:00</AcknowledgementTimeStamp>
    //   <Errors>
    //     <Error>Process Type must be 'T' when submitted to Testing Web Service</Error>
    //   </Errors>
    // </Transmission>


    CkStringBuilder_Dispose(sbXmlFileData);
    CkXml_Dispose(xml);
    CkHttp_Dispose(http);
    CkStringBuilder_Dispose(sbXml);
    CkHttpResponse_Dispose(resp);
    CkXml_Dispose(xmlResult);
    CkStringBuilder_Dispose(sbResult);

    }