Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

HttpPostJson2 Example

See more HTTP Examples

Demonstrates use of the HttpPostJson2 method.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  jsonBody: string;
  resp: THttpResponse;
  statusCode: Integer;
  json: TJsonObject;

begin
  success := False;

  //  This requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code

  //  See PostJson2Async Example for the async equivalent of this example.

  http := THttp.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..
  http.AcceptCharset := '';
  http.UserAgent := '';
  http.AcceptLanguage := '';
  http.AllowGzip := False;

  jsonBody := '{"client_id": "PLAID_CLIENT_ID", "secret":"PLAID_SECRET", "count": 10, "offset": 0}';
  resp := http.PostJson2('https://sandbox.plaid.com/institutions/get','application/json',jsonBody);
  if (http.LastMethodSuccess = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  statusCode := resp.StatusCode;
  WriteLn('Response status code = ' + statusCode);

  //  Examine the JSON response..
  json := TJsonObject.Create;
  json.Load(resp.BodyStr);
  json.EmitCompact := False;
  WriteLn('JSON Response Body:');
  WriteLn(json.Emit());

  resp.Free;


  http.Free;
  json.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.