Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
POST application/json HTTPS Request
See more HTTP Examples
Demonstrates how to send an HTTPS POST where the request body and response body both have the application/json Content-Type. Also demonstrates how to add a few custom headers to the request.Chilkat Pascal (Lazarus/Delphi) Downloads
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.HttpResponse,
Chilkat.Http;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
url: string;
jsonRequestBody: string;
resp: THttpResponse;
jsonResponseStr: string;
begin
success := False;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code
http := THttp.Create;
// Add a few custom headers.
http.SetRequestHeader('Client-ID','my_client_id');
http.SetRequestHeader('Client-Token','my_client_token');
http.Accept := 'application/json';
url := 'https://api.fiscallog.eu/sign/v1';
jsonRequestBody := '{ .... }';
resp := THttpResponse.Create;
success := http.HttpStr('POST',url,jsonRequestBody,'utf-8','application/json',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
WriteLn('Response status code = ' + resp.StatusCode);
jsonResponseStr := resp.BodyStr;
WriteLn(jsonResponseStr);
http.Free;
resp.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.