C
C
HttpPostJson2Async Example
Demonstrates use of the HttpPostJson2Async method.Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkTask.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
const char *jsonBody;
HCkTask task;
HCkHttpResponse resp;
int statusCode;
HCkJsonObject json;
success = FALSE;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code
// See PostJson2 Example for the synchronous equivalent of this example.
http = CkHttp_Create();
// Sends a POST equivalent to the following CURL command:
// curl -X POST https://sandbox.plaid.com/institutions/get \
// -H 'content-type: application/json' \
// -d '{
// "client_id": String,
// "secret":String,
// "count": Number,
// "offset": Number
// }'
// Suppress some default headers that would automatically added..
CkHttp_putAcceptCharset(http,"");
CkHttp_putUserAgent(http,"");
CkHttp_putAcceptLanguage(http,"");
CkHttp_putAllowGzip(http,FALSE);
jsonBody = "{\"client_id\": \"PLAID_CLIENT_ID\", \"secret\":\"PLAID_SECRET\", \"count\": 10, \"offset\": 0}";
task = CkHttp_PostJson2Async(http,"https://sandbox.plaid.com/institutions/get","application/json",jsonBody);
if (CkHttp_getLastMethodSuccess(http) == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
return;
}
// Start the background task.
success = CkTask_Run(task);
if (!success) {
printf("%s\n",CkTask_lastErrorText(task));
CkTask_Dispose(task);
CkHttp_Dispose(http);
return;
}
// The application is now free to do anything else
// while the HTTP POST is in progress
// For this example, we'll simply sleep and periodically
// check to see if the HTTP POST is finished.
while (CkTask_getFinished(task) != TRUE) {
// Sleep 100 ms.
CkTask_SleepMs(task,100);
}
// When we get here, the task is either finished successfully or not, or was canceled/aborted.
// If the task was "canceled", it was canceled prior to actually starting. This could
// happen if the task was canceled while waiting in a thread pool queue to be scheduled by Chilkat's
// background thread pool scheduler.
// If the task was "aborted", it indicates that it was canceled while running in a background thread.
// The ResultErrorText will likely indicate that the task was aborted.
// If the task "completed", then it ran to completion, but the actual success/failure of the method
// is determined by getting the result of the underlying method call.
// A task status of 7 indicates completion.
if (CkTask_getStatusInt(task) != 7) {
printf("Task did not complete.\n");
printf("task status: %s\n",CkTask_status(task));
CkTask_Dispose(task);
CkHttp_Dispose(http);
return;
}
// When called synchronously, the PostJson2 method returns an HTTP response object,
// unless the method failed and there was no response to be had.
// Check to see if PostJson2 method failed (and thus there is no response object).
if (CkTask_getTaskSuccess(task) == FALSE) {
printf("The underlying task failed, and there is no HTTP response object.\n");
printf("The LastErrorText for the underlying method call is:\n");
printf("%s\n",CkTask_resultErrorText(task));
CkTask_Dispose(task);
CkHttp_Dispose(http);
return;
}
// To get the HTTP response object, create a new instance and load it with the result from the task.
resp = CkHttpResponse_Create();
CkHttpResponse_LoadTaskResult(resp,task);
CkTask_Dispose(task);
statusCode = CkHttpResponse_getStatusCode(resp);
printf("Response status code = %d\n",statusCode);
// Examine the JSON response..
json = CkJsonObject_Create();
CkJsonObject_Load(json,CkHttpResponse_bodyStr(resp));
CkJsonObject_putEmitCompact(json,FALSE);
printf("JSON Response Body:\n");
printf("%s\n",CkJsonObject_emit(json));
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(json);
}