Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL nId
LOCAL cTitle
LOCAL cFirst_name
LOCAL cLast_name
LOCAL cEmail
LOCAL cPhone_office
LOCAL cPhone_mobile
LOCAL cFax
LOCAL cCreated_at
LOCAL cUpdated_at
LOCAL nClientId
LOCAL cClientName
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oHttp := CreateObject("Chilkat.Http")
// 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"
// }
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateInt("client_id", 8282839)
oJson:UpdateString("first_name", "George")
oJson:UpdateString("last_name", "Frank")
oJson:UpdateString("email", "georgefrank@example.com")
oHttp:SetRequestHeader("User-Agent", "MyApp (yourname@example.com)")
oHttp:SetRequestHeader("Content-Type", "application/json")
oHttp:SetRequestHeader("Authorization", "Bearer ACCESS_TOKEN")
oHttp:SetRequestHeader("Harvest-Account-Id", "ACCOUNT_ID")
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://api.harvestapp.com/v2/contacts", oJson, "application/json", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oJson:destroy()
oResp:destroy()
RETURN
ENDIF
oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0
? "Response Body:"
? oJResp:Emit()
nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
? "Response Header:"
? oResp:Header
? "Failed."
oHttp:destroy()
oJson:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()
RETURN
ENDIF
// 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
nId := oJResp:IntOf("id")
cTitle := oJResp:StringOf("title")
cFirst_name := oJResp:StringOf("first_name")
cLast_name := oJResp:StringOf("last_name")
cEmail := oJResp:StringOf("email")
cPhone_office := oJResp:StringOf("phone_office")
cPhone_mobile := oJResp:StringOf("phone_mobile")
cFax := oJResp:StringOf("fax")
cCreated_at := oJResp:StringOf("created_at")
cUpdated_at := oJResp:StringOf("updated_at")
nClientId := oJResp:IntOf("client.id")
cClientName := oJResp:StringOf("client.name")
oHttp:destroy()
oJson:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()