C
C
TicketBAI -- Send HTTP POST
See more TicketBAI Examples
Demonstrates how to send a TicketBAI POST and get the response.Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkStringBuilder sbXml;
HCkJsonObject json;
const char *url;
HCkHttpResponse resp;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttp_Create();
success = CkHttp_SetSslClientCertPfx(http,"your.pfx","pfx_password");
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
return;
}
// Get the XML we wish to send in the body of the request.
sbXml = CkStringBuilder_Create();
success = CkStringBuilder_LoadFile(sbXml,"qa_data/payload.xml","utf-8");
if (success == FALSE) {
printf("Failed to load XML that is to be the HTTP request body\n");
CkHttp_Dispose(http);
CkStringBuilder_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 = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"con","LROE");
CkJsonObject_UpdateString(json,"apa","1.1");
CkJsonObject_UpdateString(json,"inte.nif","número de identificación fiscal");
CkJsonObject_UpdateString(json,"inte.nrs","nombre o Razón social");
CkJsonObject_UpdateString(json,"inte.ap1","primer apellido");
CkJsonObject_UpdateString(json,"inte.ap2","segundo apellido");
CkJsonObject_UpdateString(json,"drs.mode","140");
CkJsonObject_UpdateString(json,"drs.ejer","ejercicio");
// Add required headers...
CkHttp_SetRequestHeader(http,"eus-bizkaia-n3-version","1.0");
CkHttp_SetRequestHeader(http,"eus-bizkaia-n3-content-type","application/xml");
CkHttp_SetRequestHeader(http,"eus-bizkaia-n3-data",CkJsonObject_emit(json));
url = "https://pruesarrerak.bizkaia.eus/N3B4000M/aurkezpena";
resp = CkHttpResponse_Create();
CkHttp_putUncommonOptions(http,"SendGzipped");
success = CkHttp_HttpSb(http,"POST",url,sbXml,"utf-8","application/octet-stream",resp);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbXml);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
return;
}
CkHttp_ClearHeaders(http);
printf("response status code: %d\n",CkHttpResponse_getStatusCode(resp));
// Examine the response (it is already decompressed)
printf("response body:\n");
printf("%s\n",CkHttpResponse_bodyStr(resp));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbXml);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
}