Sample code for 30+ languages & platforms
PureBasic

Constant Contact: Create a Contact

See more Constant Contact Examples

Creates a single contact.

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 -X POST \
    ;   https://api.cc.email/v3/contacts \
    ;   -H 'accept: application/json' \
    ;   -H 'authorization: Bearer {access_token}' \
    ;   -H 'cache-control: no-cache' \
    ;   -H 'content-type: application/json' \
    ;   -d '{
    ;   "email_address": {
    ;     "address": "danipper@example.com",
    ;     "permission_to_send": "implicit"
    ;   },
    ;   "first_name": "David",
    ;   "last_name": "Nipper",
    ;   "job_title": "Musician",
    ;   "company_name": "Acme Corp.",
    ;   "create_source": "Account",
    ;   "birthday_month": 11,
    ;   "birthday_day": 24,
    ;   "anniversary": "2006-11-15",
    ;   "steet_addresses":{
    ;     "kind":"home",
    ;     "street": "123 Kashmir Valley Road",
    ;     "city": "Chicago",
    ;     "state": "Illinois",
    ;     "country": "United States",
    ;   }
    ; }'

    ; Use the following online tool to generate HTTP code from a CURL command
    ; Convert a cURL Command to HTTP Source Code

    ; Use this online tool to generate code from sample JSON:
    ; Generate Code to Create JSON

    ; The following JSON is sent in the request body.

    ; {
    ;   "email_address": {
    ;     "address": "danipper@example.com",
    ;     "permission_to_send": "implicit"
    ;   },
    ;   "first_name": "David",
    ;   "last_name": "Nipper",
    ;   "job_title": "Musician",
    ;   "company_name": "Acme Corp.",
    ;   "create_source": "Account",
    ;   "birthday_month": 11,
    ;   "birthday_day": 24,
    ;   "anniversary": "2006-11-15",
    ;   "steet_addresses": {
    ;     "kind": "home",
    ;     "street": "123 Kashmir Valley Road",
    ;     "city": "Chicago",
    ;     "state": "Illinois",
    ;     "country": "United States"
    ;   }
    ; }

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

    CkJsonObject::ckUpdateString(json,"email_address.address","danipper@example.com")
    CkJsonObject::ckUpdateString(json,"email_address.permission_to_send","implicit")
    CkJsonObject::ckUpdateString(json,"first_name","David")
    CkJsonObject::ckUpdateString(json,"last_name","Nipper")
    CkJsonObject::ckUpdateString(json,"job_title","Musician")
    CkJsonObject::ckUpdateString(json,"company_name","Acme Corp.")
    CkJsonObject::ckUpdateString(json,"create_source","Account")
    CkJsonObject::ckUpdateInt(json,"birthday_month",11)
    CkJsonObject::ckUpdateInt(json,"birthday_day",24)
    CkJsonObject::ckUpdateString(json,"anniversary","2006-11-15")
    CkJsonObject::ckUpdateString(json,"steet_addresses.kind","home")
    CkJsonObject::ckUpdateString(json,"steet_addresses.street","123 Kashmir Valley Road")
    CkJsonObject::ckUpdateString(json,"steet_addresses.city","Chicago")
    CkJsonObject::ckUpdateString(json,"steet_addresses.state","Illinois")
    CkJsonObject::ckUpdateString(json,"steet_addresses.country","United States")

    CkHttp::ckSetRequestHeader(http,"content-type","application/json")
    ; Adds the "Authorization: Bearer ACCESS_TOKEN" header.
    CkHttp::setCkAuthToken(http, "ACCESS_TOKEN")
    CkHttp::ckSetRequestHeader(http,"cache-control","no-cache")
    CkHttp::ckSetRequestHeader(http,"accept","application/json")

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

    success = CkHttp::ckHttpJson(http,"POST","https://api.cc.email/v3/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:
    ; (Sample code for parsing the JSON response is shown below)

    ; {
    ;   "contact_id": "{contact_id}",
    ;   "email_address": {
    ;     "address": "danipper@example.com",
    ;     "permission_to_send": "implicit",
    ;     "created_at": "2018-02-23T13:38:28-05:00",
    ;     "updated_at": "2018-02-23T13:38:28-05:00",
    ;     "opt_in_source": "Account",
    ;     "opt_in_date": "2018-02-23T13:38:28-05:00",
    ;     "confirm_status": "off"
    ;   },
    ;   "first_name": "David",
    ;   "last_name": "Nipper",
    ;   "job_title": "Musician",
    ;   "company_name": "Acme Corp.",
    ;   "birthday_month": 11,
    ;   "birthday_day": 24,
    ;   "anniversary": "2006-11-15",
    ;   "create_source": "Account",
    ;   "created_at": "2018-02-23T13:38:28-05:00",
    ;   "updated_at": "2018-02-23T13:38:28-05:00"
    ; }

    ; Sample code for parsing the JSON response...
    ; Use the following online tool to generate parsing code from sample JSON:
    ; Generate Parsing Code from JSON

    contact_id.s = CkJsonObject::ckStringOf(jResp,"contact_id")
    email_addressAddress.s = CkJsonObject::ckStringOf(jResp,"email_address.address")
    email_addressPermission_to_send.s = CkJsonObject::ckStringOf(jResp,"email_address.permission_to_send")
    email_addressCreated_at.s = CkJsonObject::ckStringOf(jResp,"email_address.created_at")
    email_addressUpdated_at.s = CkJsonObject::ckStringOf(jResp,"email_address.updated_at")
    email_addressOpt_in_source.s = CkJsonObject::ckStringOf(jResp,"email_address.opt_in_source")
    email_addressOpt_in_date.s = CkJsonObject::ckStringOf(jResp,"email_address.opt_in_date")
    email_addressConfirm_status.s = CkJsonObject::ckStringOf(jResp,"email_address.confirm_status")
    first_name.s = CkJsonObject::ckStringOf(jResp,"first_name")
    last_name.s = CkJsonObject::ckStringOf(jResp,"last_name")
    job_title.s = CkJsonObject::ckStringOf(jResp,"job_title")
    company_name.s = CkJsonObject::ckStringOf(jResp,"company_name")
    birthday_month.i = CkJsonObject::ckIntOf(jResp,"birthday_month")
    birthday_day.i = CkJsonObject::ckIntOf(jResp,"birthday_day")
    anniversary.s = CkJsonObject::ckStringOf(jResp,"anniversary")
    create_source.s = CkJsonObject::ckStringOf(jResp,"create_source")
    created_at.s = CkJsonObject::ckStringOf(jResp,"created_at")
    updated_at.s = CkJsonObject::ckStringOf(jResp,"updated_at")


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


    ProcedureReturn
EndProcedure