Chilkat Online Tools

delphiDll / New FreshBooks / Update Project

Back to Collection Items

var
http: HCkHttp;
success: Boolean;
json: HCkJsonObject;
sbRequestBody: HCkStringBuilder;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
id: Integer;
identity_id: Integer;
role: PWideChar;
first_name: PWideChar;
last_name: PWideChar;
email: PWideChar;
company: PWideChar;
active: Boolean;
name: PWideChar;
value: Boolean;
Id: Integer;
Title: PWideChar;
Description: PWideChar;
Due_date: PWideChar;
Client_id: Integer;
Internal: Boolean;
Budget: PWideChar;
Fixed_price: PWideChar;
Rate: PWideChar;
Billing_method: PWideChar;
Project_type: PWideChar;
Active: Boolean;
Complete: Boolean;
Sample: Boolean;
Created_at: PWideChar;
Updated_at: PWideChar;
Logged_duration: PWideChar;
Billed_amount: PWideChar;
Billed_status: PWideChar;
Retainer_id: PWideChar;
GroupId: Integer;
i: Integer;
count_i: Integer;

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

// {
//   "project": {
//     "title": "K2 Hiking Guide",
//     "due_date": "2019-04-20",
//     "client_id": 123
//   }
// }

json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'project.title','K2 Hiking Guide');
CkJsonObject_UpdateString(json,'project.due_date','2019-04-20');
CkJsonObject_UpdateInt(json,'project.client_id',123);

// 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/projects/business/{{businessId}}/projects/{{projectId}}',sbRequestBody,'utf-8','application/json',False,False);
if (CkHttp_getLastMethodSuccess(http) = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

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

jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));

respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkHttpResponse__header(resp));
    Memo1.Lines.Add('Failed.');
    CkHttpResponse_Dispose(resp);
    Exit;
  end;
CkHttpResponse_Dispose(resp);

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

// {
//   "project": {
//     "id": 3018266,
//     "title": "K2 Hiking Guide",
//     "description": null,
//     "due_date": "2019-04-20",
//     "client_id": 31006,
//     "internal": false,
//     "budget": null,
//     "fixed_price": "3000.00",
//     "rate": null,
//     "billing_method": null,
//     "project_type": "fixed_price",
//     "active": true,
//     "complete": false,
//     "sample": false,
//     "created_at": "2019-05-08T20:06:55",
//     "updated_at": "2019-05-08T20:07:37",
//     "logged_duration": null,
//     "services": [
//     ],
//     "billed_amount": "0.00",
//     "billed_status": "unbilled",
//     "retainer_id": null,
//     "group": {
//       "id": 5049390,
//       "members": [
//         {
//           "id": 6578346,
//           "identity_id": 37256,
//           "role": "owner",
//           "first_name": "Marshall",
//           "last_name": "Johnston",
//           "email": "api.freshbooks@gmail.com",
//           "company": "Postman Sandbox",
//           "active": true
//         }
//       ],
//       "pending_invitations": [
//       ]
//     }
//   },
//   "abilities": [
//     {
//       "name": "can_edit",
//       "value": true
//     },
//     {
//       "name": "can_archive",
//       "value": true
//     },
//     {
//       "name": "can_generate_invoice",
//       "value": true
//     },
//     {
//       "name": "can_delete",
//       "value": true
//     },
//     {
//       "name": "can_invite_team_member",
//       "value": true
//     },
//     {
//       "name": "can_manage_services",
//       "value": true
//     },
//     {
//       "name": "can_edit_hourly_budget",
//       "value": true
//     },
//     {
//       "name": "can_view_fixed_price",
//       "value": true
//     },
//     {
//       "name": "can_edit_fixed_price",
//       "value": true
//     },
//     {
//       "name": "can_edit_end_date",
//       "value": true
//     },
//     {
//       "name": "can_edit_flat_rate",
//       "value": true
//     },
//     {
//       "name": "can_edit_project_type",
//       "value": true
//     },
//     {
//       "name": "can_edit_title",
//       "value": true
//     },
//     {
//       "name": "can_view_review_tab",
//       "value": true
//     },
//     {
//       "name": "can_view_discuss_tab",
//       "value": true
//     },
//     {
//       "name": "can_track_time",
//       "value": true
//     },
//     {
//       "name": "can_create_comment",
//       "value": true
//     }
//   ]
// }

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

Id := CkJsonObject_IntOf(jResp,'project.id');
Title := CkJsonObject__stringOf(jResp,'project.title');
Description := CkJsonObject__stringOf(jResp,'project.description');
Due_date := CkJsonObject__stringOf(jResp,'project.due_date');
Client_id := CkJsonObject_IntOf(jResp,'project.client_id');
Internal := CkJsonObject_BoolOf(jResp,'project.internal');
Budget := CkJsonObject__stringOf(jResp,'project.budget');
Fixed_price := CkJsonObject__stringOf(jResp,'project.fixed_price');
Rate := CkJsonObject__stringOf(jResp,'project.rate');
Billing_method := CkJsonObject__stringOf(jResp,'project.billing_method');
Project_type := CkJsonObject__stringOf(jResp,'project.project_type');
Active := CkJsonObject_BoolOf(jResp,'project.active');
Complete := CkJsonObject_BoolOf(jResp,'project.complete');
Sample := CkJsonObject_BoolOf(jResp,'project.sample');
Created_at := CkJsonObject__stringOf(jResp,'project.created_at');
Updated_at := CkJsonObject__stringOf(jResp,'project.updated_at');
Logged_duration := CkJsonObject__stringOf(jResp,'project.logged_duration');
Billed_amount := CkJsonObject__stringOf(jResp,'project.billed_amount');
Billed_status := CkJsonObject__stringOf(jResp,'project.billed_status');
Retainer_id := CkJsonObject__stringOf(jResp,'project.retainer_id');
GroupId := CkJsonObject_IntOf(jResp,'project.group.id');
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'project.services');
while i < count_i do
  begin
    CkJsonObject_putI(jResp,i);
    i := i + 1;
  end;

i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'project.group.members');
while i < count_i do
  begin
    CkJsonObject_putI(jResp,i);
    id := CkJsonObject_IntOf(jResp,'project.group.members[i].id');
    identity_id := CkJsonObject_IntOf(jResp,'project.group.members[i].identity_id');
    role := CkJsonObject__stringOf(jResp,'project.group.members[i].role');
    first_name := CkJsonObject__stringOf(jResp,'project.group.members[i].first_name');
    last_name := CkJsonObject__stringOf(jResp,'project.group.members[i].last_name');
    email := CkJsonObject__stringOf(jResp,'project.group.members[i].email');
    company := CkJsonObject__stringOf(jResp,'project.group.members[i].company');
    active := CkJsonObject_BoolOf(jResp,'project.group.members[i].active');
    i := i + 1;
  end;

i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'project.group.pending_invitations');
while i < count_i do
  begin
    CkJsonObject_putI(jResp,i);
    i := i + 1;
  end;

i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'abilities');
while i < count_i do
  begin
    CkJsonObject_putI(jResp,i);
    name := CkJsonObject__stringOf(jResp,'abilities[i].name');
    value := CkJsonObject_BoolOf(jResp,'abilities[i].value');
    i := i + 1;
  end;

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 '{
  "project": {
    "title": "K2 Hiking Guide",
    "due_date": "2019-04-20",
    "client_id": {{customerId}}
  }
}'
https://api.freshbooks.com/projects/business/{{businessId}}/projects/{{projectId}}

Postman Collection Item JSON

{
  "name": "Update Project",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"project\": {\n    \"title\": \"K2 Hiking Guide\",\n    \"due_date\": \"2019-04-20\",\n    \"client_id\": {{customerId}}\n  }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects/{{projectId}}",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "projects",
        "business",
        "{{businessId}}",
        "projects",
        "{{projectId}}"
      ]
    },
    "description": "Update project with project title. \n\n*`client_id` in this case is `customerId`*"
  },
  "response": [
    {
      "name": "Update Project",
      "originalRequest": {
        "method": "PUT",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"project\": {\n    \"title\": \"K2 Hiking Guide\",\n    \"due_date\": \"2019-04-20\",\n    \"client_id\": {{customerId}}\n  }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects/{{projectId}}",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "projects",
            "business",
            "{{businessId}}",
            "projects",
            "{{projectId}}"
          ]
        }
      },
      "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.7.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNEkINUgdbTBpAVhZWFEMCVlgURFgLAwBMERxUTVIZAwRfVQYGBFpfVFMIVApVAQsYAwNSFUMEUlVSVFNUVF5aWgcEUwAAFR1RBwhCU24="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "9"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1557346118"
        },
        {
          "key": "Retry-After",
          "value": "60"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 08 May 2018 20:07:37 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "1552"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 08 May 2019 20:07:37 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17361-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    \"project\": {\n        \"id\": 3018266,\n        \"title\": \"K2 Hiking Guide\",\n        \"description\": null,\n        \"due_date\": \"2019-04-20\",\n        \"client_id\": 31006,\n        \"internal\": false,\n        \"budget\": null,\n        \"fixed_price\": \"3000.00\",\n        \"rate\": null,\n        \"billing_method\": null,\n        \"project_type\": \"fixed_price\",\n        \"active\": true,\n        \"complete\": false,\n        \"sample\": false,\n        \"created_at\": \"2019-05-08T20:06:55\",\n        \"updated_at\": \"2019-05-08T20:07:37\",\n        \"logged_duration\": null,\n        \"services\": [],\n        \"billed_amount\": \"0.00\",\n        \"billed_status\": \"unbilled\",\n        \"retainer_id\": null,\n        \"group\": {\n            \"id\": 5049390,\n            \"members\": [\n                {\n                    \"id\": 6578346,\n                    \"identity_id\": 37256,\n                    \"role\": \"owner\",\n                    \"first_name\": \"Marshall\",\n                    \"last_name\": \"Johnston\",\n                    \"email\": \"api.freshbooks@gmail.com\",\n                    \"company\": \"Postman Sandbox\",\n                    \"active\": true\n                }\n            ],\n            \"pending_invitations\": []\n        }\n    },\n    \"abilities\": [\n        {\n            \"name\": \"can_edit\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_archive\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_generate_invoice\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_delete\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_invite_team_member\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_manage_services\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_hourly_budget\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_view_fixed_price\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_fixed_price\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_end_date\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_flat_rate\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_project_type\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_title\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_view_review_tab\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_view_discuss_tab\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_track_time\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_create_comment\",\n            \"value\": true\n        }\n    ]\n}"
    }
  ]
}