Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateInt(json,"client_id",8282839)
    CkJsonObject::ckUpdateString(json,"first_name","George")
    CkJsonObject::ckUpdateString(json,"last_name","Frank")
    CkJsonObject::ckUpdateString(json,"email","georgefrank@example.com")

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

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpJson(http,"POST","https://api.harvestapp.com/v2/contacts",json,"application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)
    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkHttpResponse::ckDispose(resp)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    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

    id.i
    title.s
    first_name.s
    last_name.s
    email.s
    phone_office.s
    phone_mobile.s
    fax.s
    created_at.s
    updated_at.s
    clientId.i
    clientName.s

    id = CkJsonObject::ckIntOf(jResp,"id")
    title = CkJsonObject::ckStringOf(jResp,"title")
    first_name = CkJsonObject::ckStringOf(jResp,"first_name")
    last_name = CkJsonObject::ckStringOf(jResp,"last_name")
    email = CkJsonObject::ckStringOf(jResp,"email")
    phone_office = CkJsonObject::ckStringOf(jResp,"phone_office")
    phone_mobile = CkJsonObject::ckStringOf(jResp,"phone_mobile")
    fax = CkJsonObject::ckStringOf(jResp,"fax")
    created_at = CkJsonObject::ckStringOf(jResp,"created_at")
    updated_at = CkJsonObject::ckStringOf(jResp,"updated_at")
    clientId = CkJsonObject::ckIntOf(jResp,"client.id")
    clientName = CkJsonObject::ckStringOf(jResp,"client.name")


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkHttpResponse::ckDispose(resp)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure