From 610c1e4108b8423c55fdb44f145c37354ce0d079 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 29 Sep 2015 16:36:17 +0200 Subject: [PATCH] Added simple FormField support --- README.md | 6 ++++++ URestemplateApp.pas | 22 +++++++++++++++++++++- restemplate.lpi | 3 ++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ffd608d..80eb0d3 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,12 @@ Once the first none-empty line is unrecognized (no command found), the parser wi `Method ` : This sets the method to be used for the call. +`FormField =` +: 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 ` : This prepares the actual call by providing the URL to be called. Variables in the form of `@` are replaced accordingly. diff --git a/URestemplateApp.pas b/URestemplateApp.pas index 62bf3bf..b0dd428 100644 --- a/URestemplateApp.pas +++ b/URestemplateApp.pas @@ -40,6 +40,7 @@ type FParser: TJTemplateParser; FHttp: TFPHTTPClient; FContent: TStringList; + FFormFields: TStringList; FFilters: TFilterList; FBeautify: Boolean; FURL: String; @@ -52,6 +53,7 @@ type procedure CmdHeader(AHeader: String); procedure CmdBasicAuth(AData: String); procedure CmdHighlight(AData: String); + procedure CmdFormField(AData: String); procedure ProcessCall(AURL: String); //Helper procedure WriteHelp; @@ -192,6 +194,11 @@ begin Result := True; CmdHighlight(Copy(ALine, 11, Length(ALine))); end else + if AnsiStartsStr('FormField ', ALine) then + begin + Result := True; + CmdFormField(Copy(ALine, 11, Length(ALine))); + end else if AnsiStartsStr('Call ', ALine) then begin Result := True; @@ -270,6 +277,13 @@ begin FFilters.Add(THighlightFilter.Create(AData)) end; +procedure TRestemplateApplication.CmdFormField(AData: String); +begin + FParser.Content := AData; + FParser.Replace; + FFormFields.Add(FParser.Content); +end; + procedure TRestemplateApplication.ProcessCall(AURL: String); var s: String; @@ -302,7 +316,11 @@ begin end; 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 on E: Exception do begin @@ -456,6 +474,7 @@ begin FSessionIni := TIniFile.Create(FConfigDir + 'session.ini'); FContent := TStringList.Create; + FFormFields := TStringList.Create; FHttp := TFPHTTPClient.Create(Self); FFilters := TFilterList.Create; FParser := TJTemplateParser.Create; @@ -465,6 +484,7 @@ destructor TRestemplateApplication.Destroy; begin FSessionIni.Free; FContent.Free; + FFormFields.Free; FHttp.Free; FFilters.Free; FParser.Free; diff --git a/restemplate.lpi b/restemplate.lpi index 6c157f9..a37c3a1 100644 --- a/restemplate.lpi +++ b/restemplate.lpi @@ -19,7 +19,8 @@ - + +