Sample code for 30+ languages & platforms
Lua

GetHarvest - Update Contact

See more GetHarvest Examples

Updates the specific contact by setting the values of the parameters passed. Any parameters not provided will be left unchanged. Returns a contact object and a 200 OK response code if the call succeeded.

Chilkat Lua Downloads

Lua
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/contacts/CONTACT_ID" \
--   -H "Authorization: Bearer ACCESS_TOKEN" \
--   -H "Harvest-Account-Id: ACCOUNT_ID" \
--   -H "User-Agent: MyApp (yourname@example.com)" \
--   -X PATCH \
--   -H "Content-Type: application/json" \
--   -d '{"title":"Owner"}'

-- Use this online tool to generate code from sample JSON:
-- Generate Code to Create JSON

-- The following JSON is sent in the request body.

-- {
--   "title": "Owner"
-- }

local json = chilkat.JsonObject.new()
json:update_string("title", "Owner")

http:set_request_header("User-Agent", "MyApp (yourname@example.com)")
http:set_request_header("Content-Type", "application/json")
http:set_request_header("Authorization", "Bearer ACCESS_TOKEN")
http:set_request_header("Harvest-Account-Id", "ACCOUNT_ID")

local sbRequestBody = chilkat.StringBuilder.new()
json:emit_sb(sbRequestBody)

local resp = chilkat.HttpResponse.new()
local success = http:http_sb("PATCH", "https://api.harvestapp.com/v2/contacts/CONTACT_ID", sbRequestBody, "utf-8", "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
    print("Response Header:")
    print(resp.header)
    print("Failed.")
    return
end

-- Sample JSON response:

-- {
--   "id": 4706510,
--   "title": "Owner",
--   "first_name": "George",
--   "last_name": "Frank",
--   "email": "georgefrank@example.com",
--   "phone_office": "",
--   "phone_mobile": "",
--   "fax": "",
--   "created_at": "2017-06-26T21:44:57Z",
--   "updated_at": "2017-06-26T21:46:48Z",
--   "client": {
--     "id": 5735776,
--     "name": "123 Industries"
--   }
-- }

-- 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 = jResp:int_of("id")
local title = jResp:string_of("title")
local first_name = jResp:string_of("first_name")
local last_name = jResp:string_of("last_name")
local email = jResp:string_of("email")
local phone_office = jResp:string_of("phone_office")
local phone_mobile = jResp:string_of("phone_mobile")
local fax = jResp:string_of("fax")
local created_at = jResp:string_of("created_at")
local updated_at = jResp:string_of("updated_at")
local clientId = jResp:int_of("client.id")
local clientName = jResp:string_of("client.name")