Chilkat Online Tools

unicodeC / New FreshBooks / Update Expense with Receipt

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    HCkHttpResponseW resp;

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

    // {
    //   "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
    //   }
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateNull(json,L"expense.transactionid");
    CkJsonObjectW_UpdateNull(json,L"expense.status");
    CkJsonObjectW_UpdateBool(json,L"expense.has_receipt",FALSE);
    CkJsonObjectW_UpdateBool(json,L"expense.always_remember",FALSE);
    CkJsonObjectW_UpdateBool(json,L"expense.is_cogs",FALSE);
    CkJsonObjectW_UpdateNull(json,L"expense.account_name");
    CkJsonObjectW_UpdateBool(json,L"expense.include_receipt",FALSE);
    CkJsonObjectW_UpdateString(json,L"expense.amount.amount",L"79.73");
    CkJsonObjectW_UpdateString(json,L"expense.amount.code",L"USD");
    CkJsonObjectW_UpdateString(json,L"expense.notes",L"Purchased Gasoline for deliveries");
    CkJsonObjectW_UpdateString(json,L"expense.vendor",L"Shell Gas Station");
    CkJsonObjectW_UpdateString(json,L"expense.date",L"2019-05-23");
    CkJsonObjectW_UpdateInt(json,L"expense.staffid",1);
    CkJsonObjectW_UpdateNull(json,L"expense.author_name");
    CkJsonObjectW_UpdateString(json,L"expense.taxName1",L"other tax");
    CkJsonObjectW_UpdateNull(json,L"expense.taxName2");
    CkJsonObjectW_UpdateString(json,L"expense.taxAmount1.amount",L"9.17");
    CkJsonObjectW_UpdateString(json,L"expense.taxAmount1.code",L"USD");
    CkJsonObjectW_UpdateInt(json,L"expense.taxPercent1",13);
    CkJsonObjectW_UpdateNull(json,L"expense.taxPercent2");
    CkJsonObjectW_UpdateNull(json,L"expense.markup_percent");
    CkJsonObjectW_UpdateNull(json,L"expense.invoiceid");
    CkJsonObjectW_UpdateString(json,L"expense.attachment.jwt",L"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6IlNjcmVlbiBTaG90IDIwMTktMDQtMjQgYXQgOS4zOS4xOSBBTS5wbmciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtMGU2ZDMwOWQ1ZTI5MzA3NmNhMGIyNDdkODFlOGJlNWE4NDZlZWE0YSIsImxlbmd0aCI6MTkxNDcyLCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTBlNmQzMDlkNWUyOTMwNzZjYTBiMjQ3ZDgxZThiZTVhODQ2ZWVhNGEifQ.SOoW3_nTUrxjYO76g1UXHPIrO_aLbTpXCHEa-ZcnejA");
    CkJsonObjectW_UpdateString(json,L"expense.attachment.media_type",L"image/png");
    CkJsonObjectW_UpdateNull(json,L"expense.attachment.expenseid");
    CkJsonObjectW_UpdateString(json,L"expense.categoryid",L"2003174");
    CkJsonObjectW_UpdateNull(json,L"expense.clientid");

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

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,sbRequestBody);

    resp = CkHttpW_PTextSb(http,L"PUT",L"https://api.freshbooks.com/accounting/account/{{accountId}}/expenses/expenses/{{expenseId}}?include%5B%5D=attachment",sbRequestBody,L"utf-8",L"application/json",FALSE,FALSE);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        return;
    }

    wprintf(L"%d\n",CkHttpResponseW_getStatusCode(resp));
    wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
    CkHttpResponseW_Dispose(resp);


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);

    }

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": [
  ]
}