Lua
Lua
GetHarvest - List Clients
See more GetHarvest Examples
Returns a list of your clients.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Implements the following CURL command:
-- curl "https://api.harvestapp.com/v2/clients" \
-- -H "Authorization: Bearer ACCESS_TOKEN" \
-- -H "Harvest-Account-Id: ACCOUNT_ID" \
-- -H "User-Agent: MyApp (yourname@example.com)"
http:set_request_header("User-Agent", "MyApp (yourname@example.com)")
http:set_request_header("Authorization", "Bearer ACCESS_TOKEN")
http:set_request_header("Harvest-Account-Id", "ACCOUNT_ID")
local sbResponseBody = chilkat.StringBuilder.new()
local success = http:quick_get_sb("https://api.harvestapp.com/v2/clients", sbResponseBody)
if not success then
print(http.last_error_text)
return
end
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = http.last_status
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(http.last_header)
print("Failed.")
return
end
-- Sample JSON response:
-- {
-- "clients": [
-- {
-- "id": 5735776,
-- "name": "123 Industries",
-- "is_active": true,
-- "address": "123 Main St.\r\nAnytown, LA 71223",
-- "created_at": "2017-06-26T21:02:12Z",
-- "updated_at": "2017-06-26T21:34:11Z",
-- "currency": "EUR"
-- },
-- {
-- "id": 5735774,
-- "name": "ABC Corp",
-- "is_active": true,
-- "address": "456 Main St.\r\nAnytown, CT 06467",
-- "created_at": "2017-06-26T21:01:52Z",
-- "updated_at": "2017-06-26T21:27:07Z",
-- "currency": "USD"
-- }
-- ],
-- "per_page": 100,
-- "total_pages": 1,
-- "total_entries": 2,
-- "next_page": null,
-- "previous_page": null,
-- "page": 1,
-- "links": {
-- "first": "https://api.harvestapp.com/v2/clients?page=1&per_page=100",
-- "next": null,
-- "previous": null,
-- "last": "https://api.harvestapp.com/v2/clients?page=1&per_page=100"
-- }
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
local id = 0
local name = ""
local is_active = false
local address = ""
local created_at = ""
local updated_at = ""
local currency = ""
local per_page = jResp:int_of("per_page")
local total_pages = jResp:int_of("total_pages")
local total_entries = jResp:int_of("total_entries")
local next_page = jResp:string_of("next_page")
local previous_page = jResp:string_of("previous_page")
local page = jResp:int_of("page")
local linksFirst = jResp:string_of("links.first")
local linksNext = jResp:string_of("links.next")
local linksPrevious = jResp:string_of("links.previous")
local linksLast = jResp:string_of("links.last")
local i = 0
local count_i = jResp:size_of_array("clients")
while i < count_i do
jResp.i = i
id = jResp:int_of("clients[i].id")
name = jResp:string_of("clients[i].name")
is_active = jResp:bool_of("clients[i].is_active")
address = jResp:string_of("clients[i].address")
created_at = jResp:string_of("clients[i].created_at")
updated_at = jResp:string_of("clients[i].updated_at")
currency = jResp:string_of("clients[i].currency")
i = i + 1
end