Unicode C
Unicode C
GetHarvest - Create Contact
See more GetHarvest Examples
Creates a new contact object. Returns a contact object and a 201 Created response code if the call succeeded.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkJsonObjectW json;
HCkHttpResponseW resp;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
int id;
const wchar_t *title;
const wchar_t *first_name;
const wchar_t *last_name;
const wchar_t *email;
const wchar_t *phone_office;
const wchar_t *phone_mobile;
const wchar_t *fax;
const wchar_t *created_at;
const wchar_t *updated_at;
int clientId;
const wchar_t *clientName;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// Implements the following CURL command:
// curl "https://api.harvestapp.com/v2/contacts" \
// -H "Authorization: Bearer ACCESS_TOKEN" \
// -H "Harvest-Account-Id: ACCOUNT_ID" \
// -H "User-Agent: MyApp (yourname@example.com)" \
// -X POST \
// -H "Content-Type: application/json" \
// -d '{"client_id":8282839,"first_name":"George","last_name":"Frank","email":"georgefrank@example.com"}'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "client_id": 8282839,
// "first_name": "George",
// "last_name": "Frank",
// "email": "georgefrank@example.com"
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateInt(json,L"client_id",8282839);
CkJsonObjectW_UpdateString(json,L"first_name",L"George");
CkJsonObjectW_UpdateString(json,L"last_name",L"Frank");
CkJsonObjectW_UpdateString(json,L"email",L"georgefrank@example.com");
CkHttpW_SetRequestHeader(http,L"User-Agent",L"MyApp (yourname@example.com)");
CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
CkHttpW_SetRequestHeader(http,L"Authorization",L"Bearer ACCESS_TOKEN");
CkHttpW_SetRequestHeader(http,L"Harvest-Account-Id",L"ACCOUNT_ID");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpJson(http,L"POST",L"https://api.harvestapp.com/v2/contacts",json,L"application/json",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
return;
}
sbResponseBody = CkStringBuilderW_Create();
CkHttpResponseW_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jResp,sbResponseBody);
CkJsonObjectW_putEmitCompact(jResp,FALSE);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
respStatusCode = CkHttpResponseW_getStatusCode(resp);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkHttpResponseW_header(resp));
wprintf(L"Failed.\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
// Sample JSON response:
// {
// "id": 4706510,
// "title": null,
// "first_name": "George",
// "last_name": "Frank",
// "email": "georgefrank@example.com",
// "phone_office": "",
// "phone_mobile": "",
// "fax": "",
// "created_at": "2017-06-26T21:44:57Z",
// "updated_at": "2017-06-26T21:44:57Z",
// "client": {
// "id": 5735776,
// "name": "123 Industries"
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
id = CkJsonObjectW_IntOf(jResp,L"id");
title = CkJsonObjectW_stringOf(jResp,L"title");
first_name = CkJsonObjectW_stringOf(jResp,L"first_name");
last_name = CkJsonObjectW_stringOf(jResp,L"last_name");
email = CkJsonObjectW_stringOf(jResp,L"email");
phone_office = CkJsonObjectW_stringOf(jResp,L"phone_office");
phone_mobile = CkJsonObjectW_stringOf(jResp,L"phone_mobile");
fax = CkJsonObjectW_stringOf(jResp,L"fax");
created_at = CkJsonObjectW_stringOf(jResp,L"created_at");
updated_at = CkJsonObjectW_stringOf(jResp,L"updated_at");
clientId = CkJsonObjectW_IntOf(jResp,L"client.id");
clientName = CkJsonObjectW_stringOf(jResp,L"client.name");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}