Sample code for 30+ languages & platforms
Unicode C

TicketBAI -- Send HTTP POST

See more TicketBAI Examples

Demonstrates how to send a TicketBAI POST and get the response.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkStringBuilderW sbXml;
    HCkJsonObjectW json;
    const wchar_t *url;
    HCkHttpResponseW resp;

    success = FALSE;

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

    http = CkHttpW_Create();

    success = CkHttpW_SetSslClientCertPfx(http,L"your.pfx",L"pfx_password");
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        return;
    }

    // Get the XML we wish to send in the body of the request.
    sbXml = CkStringBuilderW_Create();
    success = CkStringBuilderW_LoadFile(sbXml,L"qa_data/payload.xml",L"utf-8");
    if (success == FALSE) {
        wprintf(L"Failed to load XML that is to be the HTTP request body\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    // Build the following JSON

    // { 
    //     "con": "LROE", 
    //     "apa": "1.1", 
    //     "inte": { 
    //         "nif": "número de identificación fiscal", 
    //         "nrs": "nombre o Razón social", 
    //         "ap1": "primer apellido", 
    //         "ap2": "segundo apellido" 
    //     }, 
    //     "drs": { 
    //     "mode": "140", 
    //     "ejer": "ejercicio" 
    //     } 
    // }

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

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"con",L"LROE");
    CkJsonObjectW_UpdateString(json,L"apa",L"1.1");
    CkJsonObjectW_UpdateString(json,L"inte.nif",L"número de identificación fiscal");
    CkJsonObjectW_UpdateString(json,L"inte.nrs",L"nombre o Razón social");
    CkJsonObjectW_UpdateString(json,L"inte.ap1",L"primer apellido");
    CkJsonObjectW_UpdateString(json,L"inte.ap2",L"segundo apellido");
    CkJsonObjectW_UpdateString(json,L"drs.mode",L"140");
    CkJsonObjectW_UpdateString(json,L"drs.ejer",L"ejercicio");

    // Add required headers...
    CkHttpW_SetRequestHeader(http,L"eus-bizkaia-n3-version",L"1.0");
    CkHttpW_SetRequestHeader(http,L"eus-bizkaia-n3-content-type",L"application/xml");
    CkHttpW_SetRequestHeader(http,L"eus-bizkaia-n3-data",CkJsonObjectW_emit(json));

    url = L"https://pruesarrerak.bizkaia.eus/N3B4000M/aurkezpena";
    resp = CkHttpResponseW_Create();
    CkHttpW_putUncommonOptions(http,L"SendGzipped");
    success = CkHttpW_HttpSb(http,L"POST",url,sbXml,L"utf-8",L"application/octet-stream",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbXml);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    CkHttpW_ClearHeaders(http);

    wprintf(L"response status code: %d\n",CkHttpResponseW_getStatusCode(resp));

    // Examine the response (it is already decompressed)
    wprintf(L"response body:\n");
    wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));


    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(sbXml);
    CkJsonObjectW_Dispose(json);
    CkHttpResponseW_Dispose(resp);

    }