Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) REST POST JSON using Gzip Content EncodingDemonstrates how to send a JSON POST using the gzip Content-Encoding.
IncludeFile "CkStringBuilder.pb" IncludeFile "CkRest.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() ; 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.i = 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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.