Chilkat Online Tools

C / New FreshBooks / Update Retainer

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkJsonObject json;
    HCkStringBuilder sbRequestBody;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    int Id;
    int Business_id;
    int System_id;
    const char *Invoice_profile_id;
    int Client_user_id;
    const char *Start_date;
    const char *Next_period_start_date;
    const char *Fee;
    const char *Excess_rate;
    int Budgeted_time;
    BOOL Active;
    const char *Created_at;
    const char *Updated_at;

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

    // {
    //   "retainer": {
    //     "client_user_id": "75162",
    //     "start_date": "2019-06-03",
    //     "next_period_start_date": "2019-07-03",
    //     "fee": 20,
    //     "excess_rate": 700,
    //     "budgeted_time": 180000,
    //     "active": true,
    //     "frequency": "m",
    //     "number_recurring": 0,
    //     "is_infinitely_recurring": true
    //   }
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"retainer.client_user_id","75162");
    CkJsonObject_UpdateString(json,"retainer.start_date","2019-06-03");
    CkJsonObject_UpdateString(json,"retainer.next_period_start_date","2019-07-03");
    CkJsonObject_UpdateInt(json,"retainer.fee",20);
    CkJsonObject_UpdateInt(json,"retainer.excess_rate",700);
    CkJsonObject_UpdateInt(json,"retainer.budgeted_time",180000);
    CkJsonObject_UpdateBool(json,"retainer.active",TRUE);
    CkJsonObject_UpdateString(json,"retainer.frequency","m");
    CkJsonObject_UpdateInt(json,"retainer.number_recurring",0);
    CkJsonObject_UpdateBool(json,"retainer.is_infinitely_recurring",TRUE);

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

    sbRequestBody = CkStringBuilder_Create();
    CkJsonObject_EmitSb(json,sbRequestBody);

    resp = CkHttp_PTextSb(http,"PUT","https://api.freshbooks.com/comments/business/{{businessId}}/retainer/{{retainerId}}",sbRequestBody,"utf-8","application/json",FALSE,FALSE);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        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(sbRequestBody);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

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

    // {
    //   "retainer": {
    //     "id": 2248,
    //     "business_id": 1966214,
    //     "system_id": 4236410,
    //     "invoice_profile_id": null,
    //     "client_user_id": 75162,
    //     "start_date": "2019-06-03",
    //     "next_period_start_date": "2019-07-03",
    //     "fee": "20.00",
    //     "excess_rate": "700.00",
    //     "budgeted_time": 180000,
    //     "active": true,
    //     "created_at": "2019-06-03T15:42:49",
    //     "updated_at": "2019-06-03T18:18:48"
    //   }
    // }

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


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

    }

Curl Command

curl -X PUT
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
    "retainer": {
        "client_user_id": "75162",
        "start_date": "2019-06-03",
        "next_period_start_date": "2019-07-03",
        "fee": 20,
        "excess_rate": 700,
        "budgeted_time": 180000,
        "active": true,
        "frequency": "m",
        "number_recurring": 0,
        "is_infinitely_recurring": true
    }
}'
https://api.freshbooks.com/comments/business/{{businessId}}/retainer/{{retainerId}}

Postman Collection Item JSON

{
  "name": "Update Retainer",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"retainer\": {\n        \"client_user_id\": \"75162\",\n        \"start_date\": \"2019-06-03\",\n        \"next_period_start_date\": \"2019-07-03\",\n        \"fee\": 20,\n        \"excess_rate\": 700,\n        \"budgeted_time\": 180000,\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}}/retainer/{{retainerId}}",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "comments",
        "business",
        "{{businessId}}",
        "retainer",
        "{{retainerId}}"
      ]
    }
  },
  "response": [
    {
      "name": "Update Retainer Success",
      "originalRequest": {
        "method": "PUT",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"retainer\": {\n        \"client_user_id\": \"75162\",\n        \"start_date\": \"2019-06-03\",\n        \"next_period_start_date\": \"2019-07-03\",\n        \"fee\": 20,\n        \"excess_rate\": 700,\n        \"budgeted_time\": 180000,\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}}/retainer/{{retainerId}}",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "comments",
            "business",
            "{{businessId}}",
            "retainer",
            "{{retainerId}}"
          ]
        }
      },
      "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": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNEFUWWQtWXUYcQQBKDkQTUAdeRFIVBwpWVkJGTVIbARlVUwUPBFpRW1IHVg5WDQENBB9WDVYdQwBaVQYCVFJUW1dRVVMABwcVSgJQWkAHOw=="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "0"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1559585988"
        },
        {
          "key": "Retry-After",
          "value": "59"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Sun, 03 Jun 2018 18:18:48 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "349"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Mon, 03 Jun 2019 18:18:48 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\": 2248,\n        \"business_id\": 1966214,\n        \"system_id\": 4236410,\n        \"invoice_profile_id\": null,\n        \"client_user_id\": 75162,\n        \"start_date\": \"2019-06-03\",\n        \"next_period_start_date\": \"2019-07-03\",\n        \"fee\": \"20.00\",\n        \"excess_rate\": \"700.00\",\n        \"budgeted_time\": 180000,\n        \"active\": true,\n        \"created_at\": \"2019-06-03T15:42:49\",\n        \"updated_at\": \"2019-06-03T18:18:48\"\n    }\n}"
    }
  ]
}