Lua
Lua
GetHarvest - Create Contact
See more GetHarvest Examples
Creates a new contact object. Returns a contact object and a 201 Created response code if the call succeeded.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/contacts" \
-- -H "Authorization: Bearer ACCESS_TOKEN" \
-- -H "Harvest-Account-Id: ACCOUNT_ID" \
-- -H "User-Agent: MyApp (yourname@example.com)" \
-- -X POST \
-- -H "Content-Type: application/json" \
-- -d '{"client_id":8282839,"first_name":"George","last_name":"Frank","email":"georgefrank@example.com"}'
-- Use this online tool to generate code from sample JSON:
-- Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "client_id": 8282839,
-- "first_name": "George",
-- "last_name": "Frank",
-- "email": "georgefrank@example.com"
-- }
local json = chilkat.JsonObject.new()
json:update_int("client_id", 8282839)
json:update_string("first_name", "George")
json:update_string("last_name", "Frank")
json:update_string("email", "georgefrank@example.com")
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 resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://api.harvestapp.com/v2/contacts", json, "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": null,
-- "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:44:57Z",
-- "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")