Chilkat Online Tools

unicodeC / New FreshBooks / Update a Time Entry

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    int Id;
    int Identity_id;
    const wchar_t *Timer;
    BOOL Is_logged;
    const wchar_t *Started_at;
    const wchar_t *Created_at;
    int Duration;
    int Client_id;
    int Project_id;
    const wchar_t *Pending_client;
    const wchar_t *Pending_project;
    const wchar_t *Pending_task;
    const wchar_t *Task_id;
    const wchar_t *Service_id;
    const wchar_t *Note;
    BOOL Active;
    BOOL Billable;
    BOOL Billed;
    BOOL Internal;
    const wchar_t *Retainer_id;

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

    http = CkHttpW_Create();

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

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

    // {
    //   "time_entry": {
    //     "is_logged": true,
    //     "duration": 600,
    //     "note": "Updated Note",
    //     "started_at": "2016-08-16T18:03:28.000Z",
    //     "timer": {
    //       "id": "2936"
    //     },
    //     "client_id": 123,
    //     "project_id": 123
    //   }
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateBool(json,L"time_entry.is_logged",TRUE);
    CkJsonObjectW_UpdateInt(json,L"time_entry.duration",600);
    CkJsonObjectW_UpdateString(json,L"time_entry.note",L"Updated Note");
    CkJsonObjectW_UpdateString(json,L"time_entry.started_at",L"2016-08-16T18:03:28.000Z");
    CkJsonObjectW_UpdateString(json,L"time_entry.timer.id",L"2936");
    CkJsonObjectW_UpdateInt(json,L"time_entry.client_id",123);
    CkJsonObjectW_UpdateInt(json,L"time_entry.project_id",123);

    // Adds the "Authorization: Bearer <access_token>" header.
    CkHttpW_putAuthToken(http,L"<access_token>");
    CkHttpW_SetRequestHeader(http,L"Content-Type",L"");

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

    resp = CkHttpW_PTextSb(http,L"PUT",L"https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries/{{timeEntryId}}",sbRequestBody,L"utf-8",L"",FALSE,FALSE);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        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");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "time_entry": {
    //     "id": 47902064,
    //     "identity_id": 1882548,
    //     "timer": null,
    //     "is_logged": true,
    //     "started_at": "2016-08-16T18:03:28Z",
    //     "created_at": "2019-04-24T18:44:50Z",
    //     "duration": 600,
    //     "client_id": 31006,
    //     "project_id": 2989082,
    //     "pending_client": null,
    //     "pending_project": null,
    //     "pending_task": null,
    //     "task_id": null,
    //     "service_id": null,
    //     "note": "Updated Note",
    //     "active": true,
    //     "billable": false,
    //     "billed": false,
    //     "internal": false,
    //     "retainer_id": null
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    Id = CkJsonObjectW_IntOf(jResp,L"time_entry.id");
    Identity_id = CkJsonObjectW_IntOf(jResp,L"time_entry.identity_id");
    Timer = CkJsonObjectW_stringOf(jResp,L"time_entry.timer");
    Is_logged = CkJsonObjectW_BoolOf(jResp,L"time_entry.is_logged");
    Started_at = CkJsonObjectW_stringOf(jResp,L"time_entry.started_at");
    Created_at = CkJsonObjectW_stringOf(jResp,L"time_entry.created_at");
    Duration = CkJsonObjectW_IntOf(jResp,L"time_entry.duration");
    Client_id = CkJsonObjectW_IntOf(jResp,L"time_entry.client_id");
    Project_id = CkJsonObjectW_IntOf(jResp,L"time_entry.project_id");
    Pending_client = CkJsonObjectW_stringOf(jResp,L"time_entry.pending_client");
    Pending_project = CkJsonObjectW_stringOf(jResp,L"time_entry.pending_project");
    Pending_task = CkJsonObjectW_stringOf(jResp,L"time_entry.pending_task");
    Task_id = CkJsonObjectW_stringOf(jResp,L"time_entry.task_id");
    Service_id = CkJsonObjectW_stringOf(jResp,L"time_entry.service_id");
    Note = CkJsonObjectW_stringOf(jResp,L"time_entry.note");
    Active = CkJsonObjectW_BoolOf(jResp,L"time_entry.active");
    Billable = CkJsonObjectW_BoolOf(jResp,L"time_entry.billable");
    Billed = CkJsonObjectW_BoolOf(jResp,L"time_entry.billed");
    Internal = CkJsonObjectW_BoolOf(jResp,L"time_entry.internal");
    Retainer_id = CkJsonObjectW_stringOf(jResp,L"time_entry.retainer_id");


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -X PUT
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: "
	-d '{
  "time_entry": {
      "is_logged": true,
      "duration": 600,
      "note": "Updated Note",
      "started_at": "2016-08-16T18:03:28.000Z",
      "timer": {
          "id": "2936"
      },
      "client_id": {{clientId}},
      "project_id": {{projectId}}
  }
}'
https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries/{{timeEntryId}}

Postman Collection Item JSON

{
  "name": "Update a Time Entry",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "",
        "name": "Content-Type",
        "value": "",
        "type": "text",
        "disabled": true
      },
      {
        "key": "Content-Type",
        "value": "",
        "disabled": true
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"time_entry\": {\n      \"is_logged\": true,\n      \"duration\": 600,\n      \"note\": \"Updated Note\",\n      \"started_at\": \"2016-08-16T18:03:28.000Z\",\n      \"timer\": {\n          \"id\": \"2936\"\n      },\n      \"client_id\": {{clientId}},\n      \"project_id\": {{projectId}}\n  }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries/{{timeEntryId}}",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "timetracking",
        "business",
        "{{businessId}}",
        "time_entries",
        "{{timeEntryId}}"
      ]
    }
  },
  "response": [
    {
      "name": "Update a Time Entry",
      "originalRequest": {
        "method": "PUT",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"time_entry\": {\n      \"is_logged\": true,\n      \"duration\": 600,\n      \"note\": \"Updated Note\",\n      \"started_at\": \"2016-08-16T18:03:28.000Z\",\n      \"timer\": {\n          \"id\": \"2936\"\n      },\n      \"client_id\": {{clientId}},\n      \"project_id\": {{projectId}}\n  }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries/{{timeEntryId}}",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "timetracking",
            "business",
            "{{businessId}}",
            "time_entries",
            "{{timeEntryId}}"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1904.25.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNFlkPXT1dVkBASktLBEIORhAHUw0VDw5dbFUKFRBOExtWSAEYAlRVVlcIUghUCgACBAFUCE0DVAdORgQBUVFaCQoEVQVVDwVWBFYTGlUDChAHbQ=="
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 18:45:14 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "450"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 18:45:14 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17369-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"time_entry\": {\n        \"id\": 47902064,\n        \"identity_id\": 1882548,\n        \"timer\": null,\n        \"is_logged\": true,\n        \"started_at\": \"2016-08-16T18:03:28Z\",\n        \"created_at\": \"2019-04-24T18:44:50Z\",\n        \"duration\": 600,\n        \"client_id\": 31006,\n        \"project_id\": 2989082,\n        \"pending_client\": null,\n        \"pending_project\": null,\n        \"pending_task\": null,\n        \"task_id\": null,\n        \"service_id\": null,\n        \"note\": \"Updated Note\",\n        \"active\": true,\n        \"billable\": false,\n        \"billed\": false,\n        \"internal\": false,\n        \"retainer_id\": null\n    }\n}"
    }
  ]
}