Chilkat Online Tools

delphiDll / New FreshBooks / Update Expense

Back to Collection Items

var
http: HCkHttp;
success: Boolean;
json: HCkJsonObject;
sbRequestBody: HCkStringBuilder;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
Account_name: PWideChar;
Accountid: PWideChar;
Accounting_systemid: PWideChar;
Amount: PWideChar;
Code: PWideChar;
Background_jobid: PWideChar;
Bank_name: PWideChar;
Categoryid: Integer;
Clientid: Integer;
Compounded_tax: Boolean;
Date: PWideChar;
Expenseid: Integer;
Ext_invoiceid: Integer;
Ext_systemid: Integer;
From_bulk_import: Boolean;
Has_receipt: Boolean;
Id: Integer;
Include_receipt: Boolean;
Invoiceid: PWideChar;
Is_cogs: Boolean;
Isduplicate: Boolean;
Markup_percent: PWideChar;
Notes: PWideChar;
Profileid: PWideChar;
Projectid: Integer;
Staffid: Integer;
Status: Integer;
TaxAmount1Amount: PWideChar;
TaxAmount1Code: PWideChar;
TaxAmount2: PWideChar;
TaxName1: PWideChar;
TaxName2: PWideChar;
TaxPercent1: PWideChar;
TaxPercent2: PWideChar;
Transactionid: PWideChar;
Updated: PWideChar;
Vendor: PWideChar;
Vis_state: 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.

// {
//   "expense": {
//     "vendor": "WestJet"
//   }
// }

json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'expense.vendor','WestJet');

// 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/accounting/account/{{accountId}}/expenses/expenses/{{expenseId}}',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)

// {
//   "response": {
//     "result": {
//       "expense": {
//         "account_name": "",
//         "accountid": null,
//         "accounting_systemid": "wkMd2g",
//         "amount": {
//           "amount": "79.73",
//           "code": "USD"
//         },
//         "background_jobid": null,
//         "bank_name": "",
//         "categoryid": 2003174,
//         "clientid": 0,
//         "compounded_tax": false,
//         "date": "2019-04-24",
//         "expenseid": 1825568,
//         "ext_invoiceid": 0,
//         "ext_systemid": 0,
//         "from_bulk_import": false,
//         "has_receipt": true,
//         "id": 1825568,
//         "include_receipt": false,
//         "invoiceid": null,
//         "is_cogs": false,
//         "isduplicate": true,
//         "markup_percent": "0",
//         "notes": "Purchased Gasoline for deliveries",
//         "profileid": null,
//         "projectid": 0,
//         "staffid": 1,
//         "status": 0,
//         "taxAmount1": {
//           "amount": "9.17",
//           "code": "USD"
//         },
//         "taxAmount2": null,
//         "taxName1": "other tax",
//         "taxName2": null,
//         "taxPercent1": "13",
//         "taxPercent2": null,
//         "transactionid": null,
//         "updated": "2019-05-08 16:03:24",
//         "vendor": "WestJet",
//         "vis_state": 0
//       }
//     }
//   }
// }

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

Account_name := CkJsonObject__stringOf(jResp,'response.result.expense.account_name');
Accountid := CkJsonObject__stringOf(jResp,'response.result.expense.accountid');
Accounting_systemid := CkJsonObject__stringOf(jResp,'response.result.expense.accounting_systemid');
Amount := CkJsonObject__stringOf(jResp,'response.result.expense.amount.amount');
Code := CkJsonObject__stringOf(jResp,'response.result.expense.amount.code');
Background_jobid := CkJsonObject__stringOf(jResp,'response.result.expense.background_jobid');
Bank_name := CkJsonObject__stringOf(jResp,'response.result.expense.bank_name');
Categoryid := CkJsonObject_IntOf(jResp,'response.result.expense.categoryid');
Clientid := CkJsonObject_IntOf(jResp,'response.result.expense.clientid');
Compounded_tax := CkJsonObject_BoolOf(jResp,'response.result.expense.compounded_tax');
Date := CkJsonObject__stringOf(jResp,'response.result.expense.date');
Expenseid := CkJsonObject_IntOf(jResp,'response.result.expense.expenseid');
Ext_invoiceid := CkJsonObject_IntOf(jResp,'response.result.expense.ext_invoiceid');
Ext_systemid := CkJsonObject_IntOf(jResp,'response.result.expense.ext_systemid');
From_bulk_import := CkJsonObject_BoolOf(jResp,'response.result.expense.from_bulk_import');
Has_receipt := CkJsonObject_BoolOf(jResp,'response.result.expense.has_receipt');
Id := CkJsonObject_IntOf(jResp,'response.result.expense.id');
Include_receipt := CkJsonObject_BoolOf(jResp,'response.result.expense.include_receipt');
Invoiceid := CkJsonObject__stringOf(jResp,'response.result.expense.invoiceid');
Is_cogs := CkJsonObject_BoolOf(jResp,'response.result.expense.is_cogs');
Isduplicate := CkJsonObject_BoolOf(jResp,'response.result.expense.isduplicate');
Markup_percent := CkJsonObject__stringOf(jResp,'response.result.expense.markup_percent');
Notes := CkJsonObject__stringOf(jResp,'response.result.expense.notes');
Profileid := CkJsonObject__stringOf(jResp,'response.result.expense.profileid');
Projectid := CkJsonObject_IntOf(jResp,'response.result.expense.projectid');
Staffid := CkJsonObject_IntOf(jResp,'response.result.expense.staffid');
Status := CkJsonObject_IntOf(jResp,'response.result.expense.status');
TaxAmount1Amount := CkJsonObject__stringOf(jResp,'response.result.expense.taxAmount1.amount');
TaxAmount1Code := CkJsonObject__stringOf(jResp,'response.result.expense.taxAmount1.code');
TaxAmount2 := CkJsonObject__stringOf(jResp,'response.result.expense.taxAmount2');
TaxName1 := CkJsonObject__stringOf(jResp,'response.result.expense.taxName1');
TaxName2 := CkJsonObject__stringOf(jResp,'response.result.expense.taxName2');
TaxPercent1 := CkJsonObject__stringOf(jResp,'response.result.expense.taxPercent1');
TaxPercent2 := CkJsonObject__stringOf(jResp,'response.result.expense.taxPercent2');
Transactionid := CkJsonObject__stringOf(jResp,'response.result.expense.transactionid');
Updated := CkJsonObject__stringOf(jResp,'response.result.expense.updated');
Vendor := CkJsonObject__stringOf(jResp,'response.result.expense.vendor');
Vis_state := CkJsonObject_IntOf(jResp,'response.result.expense.vis_state');

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 '{
  "expense": {
    "vendor": "WestJet"
  }
}'
https://api.freshbooks.com/accounting/account/{{accountId}}/expenses/expenses/{{expenseId}}

Postman Collection Item JSON

{
  "name": "Update Expense",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"expense\": {\n    \"vendor\": \"WestJet\"\n  }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/expenses/expenses/{{expenseId}}",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountId}}",
        "expenses",
        "expenses",
        "{{expenseId}}"
      ]
    }
  },
  "response": [
    {
      "name": "Update Expense",
      "originalRequest": {
        "method": "PUT",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"expense\": {\n    \"vendor\": \"WestJet\"\n  }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/expenses/expenses/{{expenseId}}",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "expenses",
            "expenses",
            "{{expenseId}}"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gBSUZWDBUGEQoHQBJdVkdXEUkJTQFPAVRRDwZYUFsNAAZXVlQOBARKUAcaEVJWUAEHUwtUXl0GUAdSCAcTTVUDCEVSPA=="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "0"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1557345847"
        },
        {
          "key": "Retry-After",
          "value": "42"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 08 May 2018 20:03:24 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 08 May 2019 20:03:24 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": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"response\": {\n        \"result\": {\n            \"expense\": {\n                \"account_name\": \"\",\n                \"accountid\": null,\n                \"accounting_systemid\": \"wkMd2g\",\n                \"amount\": {\n                    \"amount\": \"79.73\",\n                    \"code\": \"USD\"\n                },\n                \"background_jobid\": null,\n                \"bank_name\": \"\",\n                \"categoryid\": 2003174,\n                \"clientid\": 0,\n                \"compounded_tax\": false,\n                \"date\": \"2019-04-24\",\n                \"expenseid\": 1825568,\n                \"ext_invoiceid\": 0,\n                \"ext_systemid\": 0,\n                \"from_bulk_import\": false,\n                \"has_receipt\": true,\n                \"id\": 1825568,\n                \"include_receipt\": false,\n                \"invoiceid\": null,\n                \"is_cogs\": false,\n                \"isduplicate\": true,\n                \"markup_percent\": \"0\",\n                \"notes\": \"Purchased Gasoline for deliveries\",\n                \"profileid\": null,\n                \"projectid\": 0,\n                \"staffid\": 1,\n                \"status\": 0,\n                \"taxAmount1\": {\n                    \"amount\": \"9.17\",\n                    \"code\": \"USD\"\n                },\n                \"taxAmount2\": null,\n                \"taxName1\": \"other tax\",\n                \"taxName2\": null,\n                \"taxPercent1\": \"13\",\n                \"taxPercent2\": null,\n                \"transactionid\": null,\n                \"updated\": \"2019-05-08 16:03:24\",\n                \"vendor\": \"WestJet\",\n                \"vis_state\": 0\n            }\n        }\n    }\n}"
    }
  ]
}