Added simple FormField support

This commit is contained in:
Andreas Schneider 2015-09-29 16:36:17 +02:00
parent aa8c6d8b58
commit 610c1e4108
3 changed files with 29 additions and 2 deletions

View File

@ -97,6 +97,12 @@ Once the first none-empty line is unrecognized (no command found), the parser wi
`Method <HTTP Method>` `Method <HTTP Method>`
: This sets the method to be used for the call. : This sets the method to be used for the call.
`FormField <key>=<value>`
: Adds a key/value pair to the url-encoded-form-data body.
The encoding will be done automatically!
This can be repeated multiple times but will only be used when Method == POST.
The Content-Type header is set automatically too.
`Call <URL>` `Call <URL>`
: This prepares the actual call by providing the URL to be called. : This prepares the actual call by providing the URL to be called.
Variables in the form of `@<variablename>` are replaced accordingly. Variables in the form of `@<variablename>` are replaced accordingly.

View File

@ -40,6 +40,7 @@ type
FParser: TJTemplateParser; FParser: TJTemplateParser;
FHttp: TFPHTTPClient; FHttp: TFPHTTPClient;
FContent: TStringList; FContent: TStringList;
FFormFields: TStringList;
FFilters: TFilterList; FFilters: TFilterList;
FBeautify: Boolean; FBeautify: Boolean;
FURL: String; FURL: String;
@ -52,6 +53,7 @@ type
procedure CmdHeader(AHeader: String); procedure CmdHeader(AHeader: String);
procedure CmdBasicAuth(AData: String); procedure CmdBasicAuth(AData: String);
procedure CmdHighlight(AData: String); procedure CmdHighlight(AData: String);
procedure CmdFormField(AData: String);
procedure ProcessCall(AURL: String); procedure ProcessCall(AURL: String);
//Helper //Helper
procedure WriteHelp; procedure WriteHelp;
@ -192,6 +194,11 @@ begin
Result := True; Result := True;
CmdHighlight(Copy(ALine, 11, Length(ALine))); CmdHighlight(Copy(ALine, 11, Length(ALine)));
end else end else
if AnsiStartsStr('FormField ', ALine) then
begin
Result := True;
CmdFormField(Copy(ALine, 11, Length(ALine)));
end else
if AnsiStartsStr('Call ', ALine) then if AnsiStartsStr('Call ', ALine) then
begin begin
Result := True; Result := True;
@ -270,6 +277,13 @@ begin
FFilters.Add(THighlightFilter.Create(AData)) FFilters.Add(THighlightFilter.Create(AData))
end; end;
procedure TRestemplateApplication.CmdFormField(AData: String);
begin
FParser.Content := AData;
FParser.Replace;
FFormFields.Add(FParser.Content);
end;
procedure TRestemplateApplication.ProcessCall(AURL: String); procedure TRestemplateApplication.ProcessCall(AURL: String);
var var
s: String; s: String;
@ -302,7 +316,11 @@ begin
end; end;
try try
FHttp.HTTPMethod(FMethod, AURL, response, []); //Special handling for formdata
if SameText('POST', FMethod) and (FFormFields.Count > 0) then
FHttp.FormPost(AURL, FFormFields, response)
else
FHttp.HTTPMethod(FMethod, AURL, response, []);
except except
on E: Exception do on E: Exception do
begin begin
@ -456,6 +474,7 @@ begin
FSessionIni := TIniFile.Create(FConfigDir + 'session.ini'); FSessionIni := TIniFile.Create(FConfigDir + 'session.ini');
FContent := TStringList.Create; FContent := TStringList.Create;
FFormFields := TStringList.Create;
FHttp := TFPHTTPClient.Create(Self); FHttp := TFPHTTPClient.Create(Self);
FFilters := TFilterList.Create; FFilters := TFilterList.Create;
FParser := TJTemplateParser.Create; FParser := TJTemplateParser.Create;
@ -465,6 +484,7 @@ destructor TRestemplateApplication.Destroy;
begin begin
FSessionIni.Free; FSessionIni.Free;
FContent.Free; FContent.Free;
FFormFields.Free;
FHttp.Free; FHttp.Free;
FFilters.Free; FFilters.Free;
FParser.Free; FParser.Free;

View File

@ -19,7 +19,8 @@
<VersionInfo> <VersionInfo>
<UseVersionInfo Value="True"/> <UseVersionInfo Value="True"/>
<MinorVersionNr Value="5"/> <MinorVersionNr Value="5"/>
<StringTable LegalCopyright="Andreas Schneider" ProductName="restemplate" ProductVersion="0.5"/> <RevisionNr Value="1"/>
<StringTable LegalCopyright="Andreas Schneider" ProductName="restemplate" ProductVersion="0.5.1"/>
</VersionInfo> </VersionInfo>
<BuildModes Count="2"> <BuildModes Count="2">
<Item1 Name="Default" Default="True"/> <Item1 Name="Default" Default="True"/>