![]() |
Chilkat • HOME • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi DLL • Go • Java • Node.js • Objective-C • PHP Extension • Perl • PowerBuilder • PowerShell • PureBasic • Ruby • SQL Server • Swift • Tcl • Unicode C • Unicode C++ • VB.NET • VBScript • Visual Basic 6.0 • Visual FoxPro • Xojo Plugin
(Delphi DLL) HttpPostJson2Async ExampleDemonstrates use of the HttpPostJson2Async method.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, Task, HttpResponse, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var http: HCkHttp; success: Boolean; jsonBody: PWideChar; task: HCkTask; resp: HCkHttpResponse; statusCode: Integer; json: HCkJsonObject; begin // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code // See PostJson2 Example for the synchronous equivalent of this example. http := CkHttp_Create(); // Sends a POST equivalent to the following CURL command: // curl -X POST https://sandbox.plaid.com/institutions/get \ // -H 'content-type: application/json' \ // -d '{ // "client_id": String, // "secret":String, // "count": Number, // "offset": Number // }' // Suppress some default headers that would automatically added.. CkHttp_putAcceptCharset(http,''); CkHttp_putUserAgent(http,''); CkHttp_putAcceptLanguage(http,''); CkHttp_putAllowGzip(http,False); jsonBody := '{"client_id": "PLAID_CLIENT_ID", "secret":"PLAID_SECRET", "count": 10, "offset": 0}'; task := CkHttp_PostJson2Async(http,'https://sandbox.plaid.com/institutions/get','application/json',jsonBody); if (CkHttp_getLastMethodSuccess(http) = False) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; // Start the background task. success := CkTask_Run(task); if (not success) then begin Memo1.Lines.Add(CkTask__lastErrorText(task)); CkTask_Dispose(task); Exit; end; // The application is now free to do anything else // while the HTTP POST is in progress // For this example, we'll simply sleep and periodically // check to see if the HTTP POST is finished. while CkTask_getFinished(task) <> True do begin // Sleep 100 ms. CkTask_SleepMs(task,100); end; // When we get here, the task is either finished successfully or not, or was canceled/aborted. // If the task was "canceled", it was canceled prior to actually starting. This could // happen if the task was canceled while waiting in a thread pool queue to be scheduled by Chilkat's // background thread pool scheduler. // If the task was "aborted", it indicates that it was canceled while running in a background thread. // The ResultErrorText will likely indicate that the task was aborted. // If the task "completed", then it ran to completion, but the actual success/failure of the method // is determined by getting the result of the underlying method call. // A task status of 7 indicates completion. if (CkTask_getStatusInt(task) <> 7) then begin Memo1.Lines.Add('Task did not complete.'); Memo1.Lines.Add('task status: ' + CkTask__status(task)); CkTask_Dispose(task); Exit; end; // When called synchronously, the PostJson2 method returns an HTTP response object, // unless the method failed and there was no response to be had. // Check to see if PostJson2 method failed (and thus there is no response object). if (CkTask_getTaskSuccess(task) = False) then begin Memo1.Lines.Add('The underlying task failed, and there is no HTTP response object.'); Memo1.Lines.Add('The LastErrorText for the underlying method call is:'); Memo1.Lines.Add(CkTask__resultErrorText(task)); CkTask_Dispose(task); Exit; end; // To get the HTTP response object, create a new instance and load it with the result from the task. resp := CkHttpResponse_Create(); CkHttpResponse_LoadTaskResult(resp,task); CkTask_Dispose(task); statusCode := CkHttpResponse_getStatusCode(resp); Memo1.Lines.Add('Response status code = ' + IntToStr(statusCode)); // Examine the JSON response.. json := CkJsonObject_Create(); CkJsonObject_Load(json,CkHttpResponse__bodyStr(resp)); CkJsonObject_putEmitCompact(json,False); Memo1.Lines.Add('JSON Response Body:'); Memo1.Lines.Add(CkJsonObject__emit(json)); CkHttp_Dispose(http); CkHttpResponse_Dispose(resp); CkJsonObject_Dispose(json); end; |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.