Chilkat Online Tools

Objective-C / New FreshBooks / Update Expense with Receipt

Back to Collection Items

#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>
#import <CkoHttpResponse.h>

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

CkoHttp *http = [[CkoHttp alloc] init];
BOOL success;

// 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": {
//     "transactionid": null,
//     "status": null,
//     "has_receipt": false,
//     "always_remember": false,
//     "is_cogs": false,
//     "account_name": null,
//     "include_receipt": false,
//     "amount": {
//       "amount": "79.73",
//       "code": "USD"
//     },
//     "notes": "Purchased Gasoline for deliveries",
//     "vendor": "Shell Gas Station",
//     "date": "2019-05-23",
//     "staffid": 1,
//     "author_name": null,
//     "taxName1": "other tax",
//     "taxName2": null,
//     "taxAmount1": {
//       "amount": "9.17",
//       "code": "USD"
//     },
//     "taxPercent1": 13,
//     "taxPercent2": null,
//     "markup_percent": null,
//     "invoiceid": null,
//     "attachment": {
//       "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6IlNjcmVlbiBTaG90IDIwMTktMDQtMjQgYXQgOS4zOS4xOSBBTS5wbmciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtMGU2ZDMwOWQ1ZTI5MzA3NmNhMGIyNDdkODFlOGJlNWE4NDZlZWE0YSIsImxlbmd0aCI6MTkxNDcyLCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTBlNmQzMDlkNWUyOTMwNzZjYTBiMjQ3ZDgxZThiZTVhODQ2ZWVhNGEifQ.SOoW3_nTUrxjYO76g1UXHPIrO_aLbTpXCHEa-ZcnejA",
//       "media_type": "image/png",
//       "expenseid": null
//     },
//     "categoryid": "2003174",
//     "clientid": null
//   }
// }

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateNull: @"expense.transactionid"];
[json UpdateNull: @"expense.status"];
[json UpdateBool: @"expense.has_receipt" value: NO];
[json UpdateBool: @"expense.always_remember" value: NO];
[json UpdateBool: @"expense.is_cogs" value: NO];
[json UpdateNull: @"expense.account_name"];
[json UpdateBool: @"expense.include_receipt" value: NO];
[json UpdateString: @"expense.amount.amount" value: @"79.73"];
[json UpdateString: @"expense.amount.code" value: @"USD"];
[json UpdateString: @"expense.notes" value: @"Purchased Gasoline for deliveries"];
[json UpdateString: @"expense.vendor" value: @"Shell Gas Station"];
[json UpdateString: @"expense.date" value: @"2019-05-23"];
[json UpdateInt: @"expense.staffid" value: [NSNumber numberWithInt: 1]];
[json UpdateNull: @"expense.author_name"];
[json UpdateString: @"expense.taxName1" value: @"other tax"];
[json UpdateNull: @"expense.taxName2"];
[json UpdateString: @"expense.taxAmount1.amount" value: @"9.17"];
[json UpdateString: @"expense.taxAmount1.code" value: @"USD"];
[json UpdateInt: @"expense.taxPercent1" value: [NSNumber numberWithInt: 13]];
[json UpdateNull: @"expense.taxPercent2"];
[json UpdateNull: @"expense.markup_percent"];
[json UpdateNull: @"expense.invoiceid"];
[json UpdateString: @"expense.attachment.jwt" value: @"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6IlNjcmVlbiBTaG90IDIwMTktMDQtMjQgYXQgOS4zOS4xOSBBTS5wbmciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtMGU2ZDMwOWQ1ZTI5MzA3NmNhMGIyNDdkODFlOGJlNWE4NDZlZWE0YSIsImxlbmd0aCI6MTkxNDcyLCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTBlNmQzMDlkNWUyOTMwNzZjYTBiMjQ3ZDgxZThiZTVhODQ2ZWVhNGEifQ.SOoW3_nTUrxjYO76g1UXHPIrO_aLbTpXCHEa-ZcnejA"];
[json UpdateString: @"expense.attachment.media_type" value: @"image/png"];
[json UpdateNull: @"expense.attachment.expenseid"];
[json UpdateString: @"expense.categoryid" value: @"2003174"];
[json UpdateNull: @"expense.clientid"];

// Adds the "Authorization: Bearer <access_token>" header.
http.AuthToken = @"<access_token>";
[http SetRequestHeader: @"Content-Type" value: @"application/json"];

CkoStringBuilder *sbRequestBody = [[CkoStringBuilder alloc] init];
[json EmitSb: sbRequestBody];

CkoHttpResponse *resp = [http PTextSb: @"PUT" url: @"https://api.freshbooks.com/accounting/account/{{accountId}}/expenses/expenses/{{expenseId}}?include%5B%5D=attachment" textData: sbRequestBody charset: @"utf-8" contentType: @"application/json" md5: NO gzip: NO];
if (http.LastMethodSuccess == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

NSLog(@"%d",[resp.StatusCode intValue]);
NSLog(@"%@",resp.BodyStr);

Curl Command

curl -X PUT
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
    "expense": {
        "transactionid": null,
        "status": null,
        "has_receipt": false,
        "always_remember": false,
        "is_cogs": false,
        "account_name": null,
        "include_receipt": false,
        "amount": {
            "amount": "79.73",
            "code": "USD"
        },
        "notes": "Purchased Gasoline for deliveries",
        "vendor": "Shell Gas Station",
        "date": "2019-05-23",
        "staffid": 1,
        "author_name": null,
        "taxName1": "other tax",
        "taxName2": null,
        "taxAmount1": {
            "amount": "9.17",
            "code": "USD"
        },
        "taxPercent1": 13,
        "taxPercent2": null,
        "markup_percent": null,
        "invoiceid": null,
        "attachment": {
            "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6IlNjcmVlbiBTaG90IDIwMTktMDQtMjQgYXQgOS4zOS4xOSBBTS5wbmciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtMGU2ZDMwOWQ1ZTI5MzA3NmNhMGIyNDdkODFlOGJlNWE4NDZlZWE0YSIsImxlbmd0aCI6MTkxNDcyLCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTBlNmQzMDlkNWUyOTMwNzZjYTBiMjQ3ZDgxZThiZTVhODQ2ZWVhNGEifQ.SOoW3_nTUrxjYO76g1UXHPIrO_aLbTpXCHEa-ZcnejA",
            "media_type": "image/png",
            "expenseid": null
        },
        "categoryid": "2003174",
        "clientid": null
    }
}'
https://api.freshbooks.com/accounting/account/{{accountId}}/expenses/expenses/{{expenseId}}?include%5B%5D=attachment

Postman Collection Item JSON

{
  "name": "Update Expense with Receipt",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"expense\": {\n        \"transactionid\": null,\n        \"status\": null,\n        \"has_receipt\": false,\n        \"always_remember\": false,\n        \"is_cogs\": false,\n        \"account_name\": null,\n        \"include_receipt\": false,\n        \"amount\": {\n            \"amount\": \"79.73\",\n            \"code\": \"USD\"\n        },\n        \"notes\": \"Purchased Gasoline for deliveries\",\n        \"vendor\": \"Shell Gas Station\",\n        \"date\": \"2019-05-23\",\n        \"staffid\": 1,\n        \"author_name\": null,\n        \"taxName1\": \"other tax\",\n        \"taxName2\": null,\n        \"taxAmount1\": {\n            \"amount\": \"9.17\",\n            \"code\": \"USD\"\n        },\n        \"taxPercent1\": 13,\n        \"taxPercent2\": null,\n        \"markup_percent\": null,\n        \"invoiceid\": null,\n        \"attachment\": {\n            \"jwt\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6IlNjcmVlbiBTaG90IDIwMTktMDQtMjQgYXQgOS4zOS4xOSBBTS5wbmciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtMGU2ZDMwOWQ1ZTI5MzA3NmNhMGIyNDdkODFlOGJlNWE4NDZlZWE0YSIsImxlbmd0aCI6MTkxNDcyLCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTBlNmQzMDlkNWUyOTMwNzZjYTBiMjQ3ZDgxZThiZTVhODQ2ZWVhNGEifQ.SOoW3_nTUrxjYO76g1UXHPIrO_aLbTpXCHEa-ZcnejA\",\n            \"media_type\": \"image/png\",\n            \"expenseid\": null\n        },\n        \"categoryid\": \"2003174\",\n        \"clientid\": null\n    }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/expenses/expenses/{{expenseId}}?include%5B%5D=attachment",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountId}}",
        "expenses",
        "expenses",
        "{{expenseId}}"
      ],
      "query": [
        {
          "key": "include%5B%5D",
          "value": "attachment"
        }
      ]
    }
  },
  "response": [
  ]
}