-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHttpClient.pas
121 lines (112 loc) · 3.79 KB
/
HttpClient.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
unit HttpClient;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Net.URLClient,
System.Net.HttpClient, System.Net.HttpClientComponent, Vcl.StdCtrls,
Vcl.ExtCtrls, Vcl.Buttons;
type
TForm1 = class(TForm)
TopPanel: TPanel;
EdtUrl: TEdit;
BottomPanel: TPanel;
ResponseMemo: TMemo;
HTTPClient: TNetHTTPClient;
HTTPRequest: TNetHTTPRequest;
BitBtn1: TBitBtn;
BtnGet: TButton;
BtnPost: TButton;
BtnPut: TButton;
BtnDel: TButton;
BodyMemo: TMemo;
BtnResetBody: TButton;
BtnResetResponse: TButton;
lblResponseMessage: TLabel;
lblBodyRequest: TLabel;
BtnDefaultUrl: TButton;
procedure BtnGetClick(Sender: TObject);
procedure BtnPostClick(Sender: TObject);
procedure BtnPutClick(Sender: TObject);
procedure BtnDeleteClick(Sender: TObject);
procedure BtnResetResponseClick(Sender: TObject);
procedure BtnResetBodyClick(Sender: TObject);
procedure BtnDefaultUrlCkick(Sender: TObject);
procedure HTTPRequestRequestCompleted(const Sender: TObject;
const AResponse: IHTTPResponse);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
System.JSON;
{$R *.dfm}
{______________________________________________________________________________}
{______________________________________________________________________________}
{______________________________________________________________________________}
// Get Click
procedure TForm1.BtnGetClick(Sender: TObject);
begin
HTTPRequest.MethodString:= 'GET'; // HTTP Method.
HTTPRequest.URL:= EdtUrl.Text; // Assign the URL
HTTPRequest.Execute(); // execute
end;
{______________________________________________________________________________}
// Post Click
procedure TForm1.BtnPostClick(Sender: TObject);
Var
IContentStream: TStringStream;
IJSONString: TJSONString;
begin
// Get Memo input -> encode into utf8, then assign into IContentStream
IJSONString:= TJSONString.Create(BodyMemo.Lines.Text);
IContentStream:= TStringStream.Create(IJSONString.ToJSON([TJSONAncestor.TJSONOutputOption.EncodeBelow32]), TEncoding.UTF8);
IContentStream.Seek(0,TSeekOrigin.soBeginning);
HTTPRequest.Post(EdtUrl.Text, IContentStream, nil, nil);
IJSONString.Free;
end;
{______________________________________________________________________________}
// Put Click
procedure TForm1.BtnPutClick(Sender: TObject);
Var
IContentStream: TStringStream;
IJSONString: TJSONString;
begin
IJSONString:= TJSONString.Create(BodyMemo.Lines.Text);
IContentStream:= TStringStream.Create(IJSONString.ToJSON([TJSONAncestor.TJSONOutputOption.EncodeBelow32]), TEncoding.UTF8);
IContentStream.Seek(0,TSeekOrigin.soBeginning);
HTTPRequest.Put(EdtUrl.Text, IContentStream, nil, nil);
IJSONString.Free;
end;
{______________________________________________________________________________}
// Set Default URL
procedure TForm1.BtnDefaultUrlCkick(Sender: TObject);
begin
EdtUrl.Text:='http://localhost:8080/'
end;
{______________________________________________________________________________}
// Delete click
procedure TForm1.BtnDeleteClick(Sender: TObject);
begin
end;
{______________________________________________________________________________}
// Reset Body
procedure TForm1.BtnResetBodyClick(Sender: TObject);
begin
BodyMemo.Lines.Text:=EmptyStr;
end;
// Reset Response
procedure TForm1.BtnResetResponseClick(Sender: TObject);
begin
ResponseMemo.Lines.Text:=EmptyStr;
end;
{______________________________________________________________________________}
procedure TForm1.HTTPRequestRequestCompleted(const Sender: TObject;
const AResponse: IHTTPResponse);
begin
ResponseMemo.Text:= AResponse.ContentAsString();
end;
end.