Sample code for 30+ languages & platforms
Unicode C

GetHarvest - Update Contact

See more GetHarvest Examples

Updates the specific contact by setting the values of the parameters passed. Any parameters not provided will be left unchanged. Returns a contact object and a 200 OK response code if the call succeeded.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    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/CONTACT_ID" \
    //   -H "Authorization: Bearer ACCESS_TOKEN" \
    //   -H "Harvest-Account-Id: ACCOUNT_ID" \
    //   -H "User-Agent: MyApp (yourname@example.com)" \
    //   -X PATCH \
    //   -H "Content-Type: application/json" \
    //   -d '{"title":"Owner"}'

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

    // The following JSON is sent in the request body.

    // {
    //   "title": "Owner"
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"title",L"Owner");

    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");

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,sbRequestBody);

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpSb(http,L"PATCH",L"https://api.harvestapp.com/v2/contacts/CONTACT_ID",sbRequestBody,L"utf-8",L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        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);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkHttpResponseW_Dispose(resp);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    // Sample JSON response:

    // {
    //   "id": 4706510,
    //   "title": "Owner",
    //   "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:46:48Z",
    //   "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);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }