Chilkat Online Tools

unicodeC / New FreshBooks / Create Retainer

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    int Id;
    int Business_id;
    int System_id;
    const wchar_t *Invoice_profile_id;
    int Client_user_id;
    const wchar_t *Start_date;
    const wchar_t *Next_period_start_date;
    const wchar_t *Fee;
    const wchar_t *Excess_rate;
    int Budgeted_time;
    BOOL Active;
    const wchar_t *Created_at;
    const wchar_t *Updated_at;

    // 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.

    // {
    //   "retainer": {
    //     "client_user_id": "32040",
    //     "start_date": "2019-06-05",
    //     "next_period_start_date": "2019-06-03",
    //     "fee": 600,
    //     "excess_rate": 200,
    //     "budgeted_time": 144000,
    //     "active": true,
    //     "frequency": "m",
    //     "number_recurring": 0,
    //     "is_infinitely_recurring": true
    //   }
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"retainer.client_user_id",L"32040");
    CkJsonObjectW_UpdateString(json,L"retainer.start_date",L"2019-06-05");
    CkJsonObjectW_UpdateString(json,L"retainer.next_period_start_date",L"2019-06-03");
    CkJsonObjectW_UpdateInt(json,L"retainer.fee",600);
    CkJsonObjectW_UpdateInt(json,L"retainer.excess_rate",200);
    CkJsonObjectW_UpdateInt(json,L"retainer.budgeted_time",144000);
    CkJsonObjectW_UpdateBool(json,L"retainer.active",TRUE);
    CkJsonObjectW_UpdateString(json,L"retainer.frequency",L"m");
    CkJsonObjectW_UpdateInt(json,L"retainer.number_recurring",0);
    CkJsonObjectW_UpdateBool(json,L"retainer.is_infinitely_recurring",TRUE);

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

    resp = CkHttpW_PostJson3(http,L"https://api.freshbooks.com/comments/business/{{businessId}}/retainers",L"application/json",json);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        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(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "retainer": {
    //     "id": 2274,
    //     "business_id": 1966214,
    //     "system_id": 4236410,
    //     "invoice_profile_id": null,
    //     "client_user_id": 32040,
    //     "start_date": "2019-06-05",
    //     "next_period_start_date": "2019-06-03",
    //     "fee": "600.00",
    //     "excess_rate": "200.00",
    //     "budgeted_time": 144000,
    //     "active": true,
    //     "created_at": "2019-06-03T18:11:26",
    //     "updated_at": "2019-06-03T18:11:26"
    //   }
    // }

    // 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"retainer.id");
    Business_id = CkJsonObjectW_IntOf(jResp,L"retainer.business_id");
    System_id = CkJsonObjectW_IntOf(jResp,L"retainer.system_id");
    Invoice_profile_id = CkJsonObjectW_stringOf(jResp,L"retainer.invoice_profile_id");
    Client_user_id = CkJsonObjectW_IntOf(jResp,L"retainer.client_user_id");
    Start_date = CkJsonObjectW_stringOf(jResp,L"retainer.start_date");
    Next_period_start_date = CkJsonObjectW_stringOf(jResp,L"retainer.next_period_start_date");
    Fee = CkJsonObjectW_stringOf(jResp,L"retainer.fee");
    Excess_rate = CkJsonObjectW_stringOf(jResp,L"retainer.excess_rate");
    Budgeted_time = CkJsonObjectW_IntOf(jResp,L"retainer.budgeted_time");
    Active = CkJsonObjectW_BoolOf(jResp,L"retainer.active");
    Created_at = CkJsonObjectW_stringOf(jResp,L"retainer.created_at");
    Updated_at = CkJsonObjectW_stringOf(jResp,L"retainer.updated_at");


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

    }

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
    "retainer": {
        "client_user_id": "32040",
        "start_date": "2019-06-05",
        "next_period_start_date": "2019-06-03",
        "fee": 600,
        "excess_rate": 200,
        "budgeted_time": 144000,
        "active": true,
        "frequency": "m",
        "number_recurring": 0,
        "is_infinitely_recurring": true
    }
}'
https://api.freshbooks.com/comments/business/{{businessId}}/retainers

Postman Collection Item JSON

{
  "name": "Create Retainer",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"retainer\": {\n        \"client_user_id\": \"32040\",\n        \"start_date\": \"2019-06-05\",\n        \"next_period_start_date\": \"2019-06-03\",\n        \"fee\": 600,\n        \"excess_rate\": 200,\n        \"budgeted_time\": 144000,\n        \"active\": true,\n        \"frequency\": \"m\",\n        \"number_recurring\": 0,\n        \"is_infinitely_recurring\": true\n    }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/comments/business/{{businessId}}/retainers",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "comments",
        "business",
        "{{businessId}}",
        "retainers"
      ]
    }
  },
  "response": [
    {
      "name": "Create Retainer Retainer for client already exists",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"retainer\": {\n        \"client_user_id\": \"32040\",\n        \"start_date\": \"2019-06-05\",\n        \"next_period_start_date\": \"2019-06-03\",\n        \"fee\": 600,\n        \"excess_rate\": 200,\n        \"budgeted_time\": 144000,\n        \"active\": true,\n        \"frequency\": \"m\",\n        \"number_recurring\": 0,\n        \"is_infinitely_recurring\": true\n    }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/comments/business/{{businessId}}/retainers",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "comments",
            "business",
            "{{businessId}}",
            "retainers"
          ]
        }
      },
      "status": "UNPROCESSABLE ENTITY",
      "code": 422,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1905.33.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNEFUWWQtWXUYcQQBKDkQTUAdeRFIVBwpWVkJGTVIbARlWXQEPClRTU1EIUAFbDQ4EBx9WDVkdQwMHBgdTAgdSWgFUBQdTBwkVSgJQWkAHOw=="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "0"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1559585586"
        },
        {
          "key": "Retry-After",
          "value": "57"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "62"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Mon, 03 Jun 2019 18:12:08 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17376-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    \"errno\": 2001,\n    \"error\": \"Retainer for client already exists\"\n}"
    },
    {
      "name": "Create Retainer Success",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"retainer\": {\n        \"client_user_id\": \"32040\",\n        \"start_date\": \"2019-06-05\",\n        \"next_period_start_date\": \"2019-06-03\",\n        \"fee\": 600,\n        \"excess_rate\": 200,\n        \"budgeted_time\": 144000,\n        \"active\": true,\n        \"frequency\": \"m\",\n        \"number_recurring\": 0,\n        \"is_infinitely_recurring\": true\n    }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/comments/business/{{businessId}}/retainers",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "comments",
            "business",
            "{{businessId}}",
            "retainers"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1905.33.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNEFUWWQtWXUYcQQBKDkQTUAdeRFIVBwpWVkJGTVIbARlVUQkBClBWVVQFVQxSAQ4DHgBRAU0TBQUEVFACB1MCCwtVUVIGUxMbAAVdRVY/"
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "0"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1559585547"
        },
        {
          "key": "Retry-After",
          "value": "59"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Sun, 03 Jun 2018 18:11:27 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "350"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Mon, 03 Jun 2019 18:11:27 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17376-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    \"retainer\": {\n        \"id\": 2274,\n        \"business_id\": 1966214,\n        \"system_id\": 4236410,\n        \"invoice_profile_id\": null,\n        \"client_user_id\": 32040,\n        \"start_date\": \"2019-06-05\",\n        \"next_period_start_date\": \"2019-06-03\",\n        \"fee\": \"600.00\",\n        \"excess_rate\": \"200.00\",\n        \"budgeted_time\": 144000,\n        \"active\": true,\n        \"created_at\": \"2019-06-03T18:11:26\",\n        \"updated_at\": \"2019-06-03T18:11:26\"\n    }\n}"
    }
  ]
}