Sample code for 30+ languages & platforms
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

Unicode C++
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkHttpW http;

    // 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"
    // }

    CkJsonObjectW json;
    json.UpdateInt(L"client_id",8282839);
    json.UpdateString(L"first_name",L"George");
    json.UpdateString(L"last_name",L"Frank");
    json.UpdateString(L"email",L"georgefrank@example.com");

    http.SetRequestHeader(L"User-Agent",L"MyApp (yourname@example.com)");
    http.SetRequestHeader(L"Content-Type",L"application/json");
    http.SetRequestHeader(L"Authorization",L"Bearer ACCESS_TOKEN");
    http.SetRequestHeader(L"Harvest-Account-Id",L"ACCOUNT_ID");

    CkHttpResponseW resp;
    success = http.HttpJson(L"POST",L"https://api.harvestapp.com/v2/contacts",json,L"application/json",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkStringBuilderW sbResponseBody;
    resp.GetBodySb(sbResponseBody);
    CkJsonObjectW jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",resp.header());
        wprintf(L"Failed.\n");
        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

    int id;
    const wchar_t *title = 0;
    const wchar_t *first_name = 0;
    const wchar_t *last_name = 0;
    const wchar_t *email = 0;
    const wchar_t *phone_office = 0;
    const wchar_t *phone_mobile = 0;
    const wchar_t *fax = 0;
    const wchar_t *created_at = 0;
    const wchar_t *updated_at = 0;
    int clientId;
    const wchar_t *clientName = 0;

    id = jResp.IntOf(L"id");
    title = jResp.stringOf(L"title");
    first_name = jResp.stringOf(L"first_name");
    last_name = jResp.stringOf(L"last_name");
    email = jResp.stringOf(L"email");
    phone_office = jResp.stringOf(L"phone_office");
    phone_mobile = jResp.stringOf(L"phone_mobile");
    fax = jResp.stringOf(L"fax");
    created_at = jResp.stringOf(L"created_at");
    updated_at = jResp.stringOf(L"updated_at");
    clientId = jResp.IntOf(L"client.id");
    clientName = jResp.stringOf(L"client.name");
    }