Xbase++
Xbase++
REST POST JSON using Gzip Content Encoding
See more REST Examples
Demonstrates how to send a JSON POST using the gzip Content-Encoding.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL oRest
LOCAL oSbReq
LOCAL oSbResp
LOCAL oJsonResp
nSuccess := 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"
// }
//
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("lists[0].id", "1511199999")
oJson:UpdateBool("confirmed", 0)
oJson:UpdateString("email_addresses[0].email_address", "support@chilkatsoft.com")
oJson:UpdateString("first_name", "Matt")
oJson:UpdateString("last_name", "Smith")
oRest := CreateObject("Chilkat.Rest")
nSuccess := oRest:Connect("api.constantcontact.com", 443, 1, 1)
IF (nSuccess != 1)
? oRest:LastErrorText
oJson:destroy()
oRest:destroy()
RETURN
ENDIF
oRest:AddHeader("Content-Type", "application/json")
oRest:AddHeader("Authorization", "Bearer xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx")
// Tell the server you'll accept only an application/json response.
oRest:AddHeader("Accept", "application/json")
// Indicate that we'll be sending the request body gzipped.
oRest:AddHeader("Content-Encoding", "gzip")
oSbReq := CreateObject("Chilkat.StringBuilder")
oJson:EmitSb(oSbReq)
// Send the POST.
oSbResp := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestSb("POST", "/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=xxxxxxx", oSbReq, oSbResp)
IF (nSuccess != 1)
? oRest:LastErrorText
oJson:destroy()
oRest:destroy()
oSbReq:destroy()
oSbResp:destroy()
RETURN
ENDIF
? "Response body:"
? oSbResp:GetAsString()
IF (oRest:ResponseStatusCode != 200)
? "Received error response code: " + Str(oRest:ResponseStatusCode)
oJson:destroy()
oRest:destroy()
oSbReq:destroy()
oSbResp:destroy()
RETURN
ENDIF
oJsonResp := CreateObject("Chilkat.JsonObject")
oJsonResp:LoadSb(oSbResp)
// Use the online tool at https://tools.chilkat.io/jsonParse.cshtml
// to generate the JSON code that parses the JSON response..
oJson:destroy()
oRest:destroy()
oSbReq:destroy()
oSbResp:destroy()
oJsonResp:destroy()