2016-05-23 17:26:00 +02:00
|
|
|
unit MainClientFormU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2019-07-08 19:25:56 +02:00
|
|
|
Winapi.Windows,
|
|
|
|
Winapi.Messages,
|
|
|
|
System.SysUtils,
|
|
|
|
System.Variants,
|
|
|
|
System.Classes,
|
|
|
|
Vcl.Graphics,
|
|
|
|
Vcl.Controls,
|
|
|
|
Vcl.Forms,
|
|
|
|
Vcl.Dialogs,
|
|
|
|
Vcl.StdCtrls,
|
|
|
|
Vcl.ExtCtrls;
|
2016-05-23 17:26:00 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TForm5 = class(TForm)
|
2016-06-23 11:42:16 +02:00
|
|
|
Memo1: TMemo;
|
|
|
|
Memo2: TMemo;
|
|
|
|
Panel1: TPanel;
|
2016-05-23 17:26:00 +02:00
|
|
|
btnGet: TButton;
|
|
|
|
btnLOGIN: TButton;
|
2016-06-23 11:42:16 +02:00
|
|
|
Splitter1: TSplitter;
|
2017-03-10 10:37:29 +01:00
|
|
|
Memo3: TMemo;
|
|
|
|
Splitter2: TSplitter;
|
2019-07-08 19:25:56 +02:00
|
|
|
btnLoginWithException: TButton;
|
2016-05-23 17:26:00 +02:00
|
|
|
procedure btnGetClick(Sender: TObject);
|
|
|
|
procedure btnLOGINClick(Sender: TObject);
|
2019-07-08 19:25:56 +02:00
|
|
|
procedure btnLoginWithExceptionClick(Sender: TObject);
|
2016-05-23 17:26:00 +02:00
|
|
|
private
|
2017-05-17 22:32:45 +02:00
|
|
|
FJWT: string;
|
|
|
|
procedure SetJWT(const Value: string);
|
|
|
|
property JWT: string read FJWT write SetJWT;
|
2016-05-23 17:26:00 +02:00
|
|
|
public
|
|
|
|
{ Public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
Form5: TForm5;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
|
|
|
|
|
|
|
uses
|
2017-07-05 00:17:46 +02:00
|
|
|
MVCFramework.RESTClient,
|
2017-05-17 22:32:45 +02:00
|
|
|
MVCFramework.SystemJSONUtils,
|
2018-12-09 23:03:06 +01:00
|
|
|
System.JSON;
|
2016-05-23 17:26:00 +02:00
|
|
|
|
|
|
|
procedure TForm5.btnGetClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
lClient: TRESTClient;
|
2016-06-23 12:11:01 +02:00
|
|
|
lResp: IRESTResponse;
|
2016-06-23 11:42:16 +02:00
|
|
|
lQueryStringParams: TStringList;
|
2016-05-23 17:26:00 +02:00
|
|
|
begin
|
2017-03-10 10:37:29 +01:00
|
|
|
{ Getting JSON response }
|
2016-05-23 17:26:00 +02:00
|
|
|
lClient := TRESTClient.Create('localhost', 8080);
|
|
|
|
try
|
2019-06-11 19:42:03 +02:00
|
|
|
lClient.UseBasicAuthentication := False;
|
2016-06-23 11:42:16 +02:00
|
|
|
lClient.ReadTimeOut(0);
|
|
|
|
if not FJWT.IsEmpty then
|
2019-06-11 19:42:03 +02:00
|
|
|
lClient.RequestHeaders.Values['Authorization'] := 'bearer ' + FJWT;
|
2016-06-23 11:42:16 +02:00
|
|
|
lQueryStringParams := TStringList.Create;
|
|
|
|
try
|
|
|
|
lQueryStringParams.Values['firstname'] := 'Daniele';
|
|
|
|
lQueryStringParams.Values['lastname'] := 'Teti';
|
2016-06-23 12:11:01 +02:00
|
|
|
lResp := lClient.doGET('/admin/role1', [], lQueryStringParams);
|
|
|
|
if lResp.HasError then
|
|
|
|
ShowMessage(lResp.Error.ExceptionMessage);
|
2016-06-23 11:42:16 +02:00
|
|
|
finally
|
|
|
|
lQueryStringParams.Free;
|
|
|
|
end;
|
2016-06-23 12:11:01 +02:00
|
|
|
Memo2.Lines.Text := lResp.BodyAsString;
|
2016-05-23 17:26:00 +02:00
|
|
|
finally
|
|
|
|
lClient.Free;
|
|
|
|
end;
|
2017-03-10 10:37:29 +01:00
|
|
|
|
|
|
|
{ Getting HTML response }
|
|
|
|
lClient := TRESTClient.Create('localhost', 8080);
|
|
|
|
try
|
2019-06-11 19:42:03 +02:00
|
|
|
// when the JWT authorization header is named "Authorization", the basic authorization must be disabled
|
|
|
|
lClient.UseBasicAuthentication := False;
|
|
|
|
|
2017-03-10 10:37:29 +01:00
|
|
|
lClient.ReadTimeOut(0);
|
|
|
|
if not FJWT.IsEmpty then
|
2019-06-11 19:42:03 +02:00
|
|
|
lClient.RequestHeaders.Values['Authorization'] := 'bearer ' + FJWT;
|
2017-03-10 10:37:29 +01:00
|
|
|
lQueryStringParams := TStringList.Create;
|
|
|
|
try
|
|
|
|
lQueryStringParams.Values['firstname'] := 'Daniele';
|
|
|
|
lQueryStringParams.Values['lastname'] := 'Teti';
|
|
|
|
lResp := lClient.Accept('text/html').doGET('/admin/role1', [], lQueryStringParams);
|
|
|
|
if lResp.HasError then
|
|
|
|
ShowMessage(lResp.Error.ExceptionMessage);
|
|
|
|
finally
|
|
|
|
lQueryStringParams.Free;
|
|
|
|
end;
|
|
|
|
Memo3.Lines.Text := lResp.BodyAsString;
|
|
|
|
finally
|
|
|
|
lClient.Free;
|
|
|
|
end;
|
|
|
|
|
2016-05-23 17:26:00 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm5.btnLOGINClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
lClient: TRESTClient;
|
|
|
|
lRest: IRESTResponse;
|
2017-05-17 22:32:45 +02:00
|
|
|
lJSON: TJSONObject;
|
2016-05-23 17:26:00 +02:00
|
|
|
begin
|
|
|
|
lClient := TRESTClient.Create('localhost', 8080);
|
|
|
|
try
|
2016-06-23 11:42:16 +02:00
|
|
|
lClient.ReadTimeOut(0);
|
2016-05-23 17:26:00 +02:00
|
|
|
lClient
|
2019-06-11 19:42:03 +02:00
|
|
|
.Header('username', 'user1')
|
|
|
|
.Header('password', 'user1');
|
2016-05-23 17:26:00 +02:00
|
|
|
lRest := lClient.doPOST('/login', []);
|
2017-07-14 17:38:51 +02:00
|
|
|
if lRest.HasError then
|
|
|
|
begin
|
2019-07-08 19:25:56 +02:00
|
|
|
ShowMessage(
|
|
|
|
'HTTP ERROR: ' + lRest.Error.HTTPError.ToString + sLineBreak +
|
|
|
|
'APPLICATION ERROR CODE: ' + lRest.Error.ErrorNumber.ToString + sLineBreak +
|
|
|
|
'EXCEPTION MESSAGE: ' + lRest.Error.ExceptionMessage);
|
|
|
|
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
lJSON := TSystemJSON.StringAsJSONObject(lRest.BodyAsString);
|
|
|
|
try
|
|
|
|
JWT := lJSON.GetValue('token').Value;
|
|
|
|
finally
|
|
|
|
lJSON.Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lClient.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm5.btnLoginWithExceptionClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
lClient: TRESTClient;
|
|
|
|
lRest: IRESTResponse;
|
|
|
|
lJSON: TJSONObject;
|
|
|
|
begin
|
|
|
|
lClient := TRESTClient.Create('localhost', 8080);
|
|
|
|
try
|
|
|
|
lClient.ReadTimeOut(0);
|
|
|
|
lClient
|
|
|
|
.Header('username', 'user_raise_exception')
|
|
|
|
.Header('password', 'user_raise_exception');
|
|
|
|
lRest := lClient.doPOST('/login', []);
|
|
|
|
if lRest.HasError then
|
|
|
|
begin
|
|
|
|
ShowMessage(
|
|
|
|
'HTTP ERROR: ' + lRest.Error.HTTPError.ToString + sLineBreak +
|
|
|
|
'APPLICATION ERROR CODE: ' + lRest.Error.ErrorNumber.ToString + sLineBreak +
|
|
|
|
'EXCEPTION MESSAGE: ' + lRest.Error.ExceptionMessage);
|
2017-07-14 17:38:51 +02:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-07-05 00:17:46 +02:00
|
|
|
lJSON := TSystemJSON.StringAsJSONObject(lRest.BodyAsString);
|
2017-05-17 22:32:45 +02:00
|
|
|
try
|
|
|
|
JWT := lJSON.GetValue('token').Value;
|
|
|
|
finally
|
|
|
|
lJSON.Free;
|
|
|
|
end;
|
2016-05-23 17:26:00 +02:00
|
|
|
finally
|
|
|
|
lClient.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-05-17 22:32:45 +02:00
|
|
|
procedure TForm5.SetJWT(const Value: string);
|
2016-06-23 11:42:16 +02:00
|
|
|
begin
|
|
|
|
FJWT := Value;
|
|
|
|
Memo1.Lines.Text := Value;
|
|
|
|
end;
|
|
|
|
|
2016-05-23 17:26:00 +02:00
|
|
|
end.
|