PureBasic
PureBasic
REST POST JSON using Gzip Content Encoding
See more REST Examples
Demonstrates how to send a JSON POST using the gzip Content-Encoding.Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; This requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
; Use the online tool at https://tools.chilkat.io/Default.cshtml
; to generate the JSON code that creates this JSON:
; {
; "lists": [
; {
; "id": "1511199999"
; }
; ],
; "confirmed": false,
; "email_addresses": [
; {
; "email_address": "support@chilkatsoft.com"
; }
; ],
; "first_name": "Matt",
; "last_name": "Smith"
; }
;
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"lists[0].id","1511199999")
CkJsonObject::ckUpdateBool(json,"confirmed",0)
CkJsonObject::ckUpdateString(json,"email_addresses[0].email_address","support@chilkatsoft.com")
CkJsonObject::ckUpdateString(json,"first_name","Matt")
CkJsonObject::ckUpdateString(json,"last_name","Smith")
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkRest::ckConnect(rest,"api.constantcontact.com",443,1,1)
If success <> 1
Debug CkRest::ckLastErrorText(rest)
CkJsonObject::ckDispose(json)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
CkRest::ckAddHeader(rest,"Content-Type","application/json")
CkRest::ckAddHeader(rest,"Authorization","Bearer xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx")
; Tell the server you'll accept only an application/json response.
CkRest::ckAddHeader(rest,"Accept","application/json")
; Indicate that we'll be sending the request body gzipped.
CkRest::ckAddHeader(rest,"Content-Encoding","gzip")
sbReq.i = CkStringBuilder::ckCreate()
If sbReq.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckEmitSb(json,sbReq)
; Send the POST.
sbResp.i = CkStringBuilder::ckCreate()
If sbResp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkRest::ckFullRequestSb(rest,"POST","/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=xxxxxxx",sbReq,sbResp)
If success <> 1
Debug CkRest::ckLastErrorText(rest)
CkJsonObject::ckDispose(json)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(sbReq)
CkStringBuilder::ckDispose(sbResp)
ProcedureReturn
EndIf
Debug "Response body:"
Debug CkStringBuilder::ckGetAsString(sbResp)
If CkRest::ckResponseStatusCode(rest) <> 200
Debug "Received error response code: " + Str(CkRest::ckResponseStatusCode(rest))
CkJsonObject::ckDispose(json)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(sbReq)
CkStringBuilder::ckDispose(sbResp)
ProcedureReturn
EndIf
jsonResp.i = CkJsonObject::ckCreate()
If jsonResp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoadSb(jsonResp,sbResp)
; Use the online tool at https://tools.chilkat.io/jsonParse.cshtml
; to generate the JSON code that parses the JSON response..
CkJsonObject::ckDispose(json)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(sbReq)
CkStringBuilder::ckDispose(sbResp)
CkJsonObject::ckDispose(jsonResp)
ProcedureReturn
EndProcedure