Chilkat Online Tools

C / New FreshBooks / Create a Time Entry

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkJsonObject json;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    int Id;
    int Identity_id;
    const char *Timer;
    BOOL Is_logged;
    const char *Started_at;
    const char *Created_at;
    int Duration;
    int Client_id;
    int Project_id;
    const char *Pending_client;
    const char *Pending_project;
    const char *Pending_task;
    const char *Task_id;
    const char *Service_id;
    const char *Note;
    BOOL Active;
    BOOL Billable;
    BOOL Billed;
    BOOL Internal;
    const char *Retainer_id;

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

    http = CkHttp_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": 7200,
    //     "note": "Stuff",
    //     "started_at": "2016-08-16T20:00:00.000Z",
    //     "client_id": 123,
    //     "project_id": 123,
    //     "service_id": 123
    //   }
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateBool(json,"time_entry.is_logged",TRUE);
    CkJsonObject_UpdateInt(json,"time_entry.duration",7200);
    CkJsonObject_UpdateString(json,"time_entry.note","Stuff");
    CkJsonObject_UpdateString(json,"time_entry.started_at","2016-08-16T20:00:00.000Z");
    CkJsonObject_UpdateInt(json,"time_entry.client_id",123);
    CkJsonObject_UpdateInt(json,"time_entry.project_id",123);
    CkJsonObject_UpdateInt(json,"time_entry.service_id",123);

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

    resp = CkHttp_PostJson3(http,"https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries","",json);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        return;
    }

    sbResponseBody = CkStringBuilder_Create();
    CkHttpResponse_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttpResponse_header(resp));
        printf("Failed.\n");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_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-16T20:00:00Z",
    //     "created_at": "2019-04-24T18:44:50Z",
    //     "duration": 7200,
    //     "client_id": 31006,
    //     "project_id": 2989082,
    //     "pending_client": null,
    //     "pending_project": null,
    //     "pending_task": null,
    //     "task_id": null,
    //     "service_id": null,
    //     "note": "Stuff",
    //     "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 = CkJsonObject_IntOf(jResp,"time_entry.id");
    Identity_id = CkJsonObject_IntOf(jResp,"time_entry.identity_id");
    Timer = CkJsonObject_stringOf(jResp,"time_entry.timer");
    Is_logged = CkJsonObject_BoolOf(jResp,"time_entry.is_logged");
    Started_at = CkJsonObject_stringOf(jResp,"time_entry.started_at");
    Created_at = CkJsonObject_stringOf(jResp,"time_entry.created_at");
    Duration = CkJsonObject_IntOf(jResp,"time_entry.duration");
    Client_id = CkJsonObject_IntOf(jResp,"time_entry.client_id");
    Project_id = CkJsonObject_IntOf(jResp,"time_entry.project_id");
    Pending_client = CkJsonObject_stringOf(jResp,"time_entry.pending_client");
    Pending_project = CkJsonObject_stringOf(jResp,"time_entry.pending_project");
    Pending_task = CkJsonObject_stringOf(jResp,"time_entry.pending_task");
    Task_id = CkJsonObject_stringOf(jResp,"time_entry.task_id");
    Service_id = CkJsonObject_stringOf(jResp,"time_entry.service_id");
    Note = CkJsonObject_stringOf(jResp,"time_entry.note");
    Active = CkJsonObject_BoolOf(jResp,"time_entry.active");
    Billable = CkJsonObject_BoolOf(jResp,"time_entry.billable");
    Billed = CkJsonObject_BoolOf(jResp,"time_entry.billed");
    Internal = CkJsonObject_BoolOf(jResp,"time_entry.internal");
    Retainer_id = CkJsonObject_stringOf(jResp,"time_entry.retainer_id");


    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: "
	-d '{
    "time_entry": {
        "is_logged": true,
        "duration": 7200,
        "note": "Stuff",
        "started_at": "2016-08-16T20:00:00.000Z",
        "client_id": {{clientId}},
        "project_id": {{projectId}}.
        "service_id": {{serviceid}}
    }
}'
https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries

Postman Collection Item JSON

{
  "name": "Create a Time Entry",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "var jsonData = JSON.parse(responseBody);",
          "postman.setEnvironmentVariable(\"timeEntryId\", jsonData.time_entry.id);"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "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\": 7200,\n        \"note\": \"Stuff\",\n        \"started_at\": \"2016-08-16T20:00:00.000Z\",\n        \"client_id\": {{clientId}},\n        \"project_id\": {{projectId}}.\n        \"service_id\": {{serviceid}}\n    }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "timetracking",
        "business",
        "{{businessId}}",
        "time_entries"
      ]
    }
  },
  "response": [
    {
      "name": "Create a Time Entry",
      "originalRequest": {
        "method": "POST",
        "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\": 7200,\n        \"note\": \"Stuff\",\n        \"started_at\": \"2016-08-16T20:00:00.000Z\",\n        \"client_id\": {{clientId}},\n        \"project_id\": {{projectId}}\n    }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "timetracking",
            "business",
            "{{businessId}}",
            "time_entries"
          ]
        }
      },
      "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": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNFlkPXT1dVkBAWgBKT0MEQA0RRFQEXBdRXlU7BAxDQ14DFxMaA05WTVMEVg1WCA8MBARRClAHVQNUSAQGVEpBWlYHVgQABwEBVQYDUgBUBUFOVgNUEV1l"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 18:44:50 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "444"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 18:44:50 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-16T20:00:00Z\",\n        \"created_at\": \"2019-04-24T18:44:50Z\",\n        \"duration\": 7200,\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\": \"Stuff\",\n        \"active\": true,\n        \"billable\": false,\n        \"billed\": false,\n        \"internal\": false,\n        \"retainer_id\": null\n    }\n}"
    }
  ]
}