Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vJson
    Handle hoJson
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    Integer iId
    String sTitle
    String sFirst_name
    String sLast_name
    String sEmail
    String sPhone_office
    String sPhone_mobile
    String sFax
    String sCreated_at
    String sUpdated_at
    Integer iClientId
    String sClientName
    String sTemp1

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateInt Of hoJson "client_id" 8282839 To iSuccess
    Get ComUpdateString Of hoJson "first_name" "George" To iSuccess
    Get ComUpdateString Of hoJson "last_name" "Frank" To iSuccess
    Get ComUpdateString Of hoJson "email" "georgefrank@example.com" To iSuccess

    Send ComSetRequestHeader To hoHttp "User-Agent" "MyApp (yourname@example.com)"
    Send ComSetRequestHeader To hoHttp "Content-Type" "application/json"
    Send ComSetRequestHeader To hoHttp "Authorization" "Bearer ACCESS_TOKEN"
    Send ComSetRequestHeader To hoHttp "Harvest-Account-Id" "ACCOUNT_ID"

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://api.harvestapp.com/v2/contacts" vJson "application/json" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_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

    Get ComIntOf Of hoJResp "id" To iId
    Get ComStringOf Of hoJResp "title" To sTitle
    Get ComStringOf Of hoJResp "first_name" To sFirst_name
    Get ComStringOf Of hoJResp "last_name" To sLast_name
    Get ComStringOf Of hoJResp "email" To sEmail
    Get ComStringOf Of hoJResp "phone_office" To sPhone_office
    Get ComStringOf Of hoJResp "phone_mobile" To sPhone_mobile
    Get ComStringOf Of hoJResp "fax" To sFax
    Get ComStringOf Of hoJResp "created_at" To sCreated_at
    Get ComStringOf Of hoJResp "updated_at" To sUpdated_at
    Get ComIntOf Of hoJResp "client.id" To iClientId
    Get ComStringOf Of hoJResp "client.name" To sClientName


End_Procedure