Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oSbRequestBody
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/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"
//  }

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("title", "Owner")

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")

oSbRequestBody := CreateObject("Chilkat.StringBuilder")
oJson:EmitSb(oSbRequestBody)

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpSb("PATCH", "https://api.harvestapp.com/v2/contacts/CONTACT_ID", oSbRequestBody, "utf-8", "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oJson:destroy()
    oSbRequestBody: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()
    oSbRequestBody:destroy()
    oResp:destroy()
    oSbResponseBody:destroy()
    oJResp:destroy()
    RETURN
ENDIF

//  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

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()
oSbRequestBody:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()