Chilkat Online Tools

PureBasic / New FreshBooks / Update Expense with Receipt

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ; 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.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateNull(json,"expense.transactionid")
    CkJsonObject::ckUpdateNull(json,"expense.status")
    CkJsonObject::ckUpdateBool(json,"expense.has_receipt",0)
    CkJsonObject::ckUpdateBool(json,"expense.always_remember",0)
    CkJsonObject::ckUpdateBool(json,"expense.is_cogs",0)
    CkJsonObject::ckUpdateNull(json,"expense.account_name")
    CkJsonObject::ckUpdateBool(json,"expense.include_receipt",0)
    CkJsonObject::ckUpdateString(json,"expense.amount.amount","79.73")
    CkJsonObject::ckUpdateString(json,"expense.amount.code","USD")
    CkJsonObject::ckUpdateString(json,"expense.notes","Purchased Gasoline for deliveries")
    CkJsonObject::ckUpdateString(json,"expense.vendor","Shell Gas Station")
    CkJsonObject::ckUpdateString(json,"expense.date","2019-05-23")
    CkJsonObject::ckUpdateInt(json,"expense.staffid",1)
    CkJsonObject::ckUpdateNull(json,"expense.author_name")
    CkJsonObject::ckUpdateString(json,"expense.taxName1","other tax")
    CkJsonObject::ckUpdateNull(json,"expense.taxName2")
    CkJsonObject::ckUpdateString(json,"expense.taxAmount1.amount","9.17")
    CkJsonObject::ckUpdateString(json,"expense.taxAmount1.code","USD")
    CkJsonObject::ckUpdateInt(json,"expense.taxPercent1",13)
    CkJsonObject::ckUpdateNull(json,"expense.taxPercent2")
    CkJsonObject::ckUpdateNull(json,"expense.markup_percent")
    CkJsonObject::ckUpdateNull(json,"expense.invoiceid")
    CkJsonObject::ckUpdateString(json,"expense.attachment.jwt","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6IlNjcmVlbiBTaG90IDIwMTktMDQtMjQgYXQgOS4zOS4xOSBBTS5wbmciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtMGU2ZDMwOWQ1ZTI5MzA3NmNhMGIyNDdkODFlOGJlNWE4NDZlZWE0YSIsImxlbmd0aCI6MTkxNDcyLCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTBlNmQzMDlkNWUyOTMwNzZjYTBiMjQ3ZDgxZThiZTVhODQ2ZWVhNGEifQ.SOoW3_nTUrxjYO76g1UXHPIrO_aLbTpXCHEa-ZcnejA")
    CkJsonObject::ckUpdateString(json,"expense.attachment.media_type","image/png")
    CkJsonObject::ckUpdateNull(json,"expense.attachment.expenseid")
    CkJsonObject::ckUpdateString(json,"expense.categoryid","2003174")
    CkJsonObject::ckUpdateNull(json,"expense.clientid")

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

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckEmitSb(json,sbRequestBody)

    resp.i = CkHttp::ckPTextSb(http,"PUT","https://api.freshbooks.com/accounting/account/{{accountId}}/expenses/expenses/{{expenseId}}?include%5B%5D=attachment",sbRequestBody,"utf-8","application/json",0,0)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)
    CkHttpResponse::ckDispose(resp)



    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)


    ProcedureReturn
EndProcedure

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