Jump to content

Provide a plain text when a URL with params are acessed


diego@grv.com.br

Recommended Posts

Hello,

 

Can anyone help me?

 

I have a simple application with a login and a main menu form, very usually, but with a particular parameters in the URL, I need to provide a simple, unformatted text content to the respective user's web browser (that text can be a JSon, XML, OData, etc).

 

This URL contains authentication parameters (something like "127.0.0.1:8077/?authkey=700UB7BN3FM1ZMN").

 

I need to use the "UniGUIMainModuleBeforeLogin" event of the "UniMainModule", to validate this authentication key, determine which user is accessing the data and only if that key/user is validated, select their respective data and generate the plain text in the format that need.

 

What I need is simple: When this URL with parameters are acessed, after the validation on the "UniGUIMainModuleBeforeLogin" event, instead of showing the Main Menu screen, I need that this simple text (which can be a text in the OData, JSon, XML, etc.) return that plain string back to the user's web browser.

 

Attached is an image that shows what I need.

 

My UniGUI version is "FMSoft uniGUI Complete Professional 1.0.0.1423". 

 

I have tried the following approach:

 

procedure TUniMainModule.UniGUIMainModuleBeforeLogin(Sender: TObject; var Handled: Boolean);

var

   xAuthKey, xUserInfo, xStrData : string;

begin

 

     Handled := False;

 

     //Try to get authentication info

     xAuthKey := (Sender as TUniGUISession).UniApplication.Parameters.Values['odata'];

     if not xAuthKey.IsEmpty then

     begin

          //Validate authentication info and gets the user info

          xUserInfo := GetUserInfo(xAuthKey);

          if not xUserInfo.IsEmpty then

          begin

               //GetStrData returns information of database based on user info

                 //It can be a JSon, a XML or OData, or even, a simple text

               xStrData := GetStrData(xUserInfo);

 

               { Down here i got troubles }

 

               TUniGuiSession(Sender).AResponse.ContentType := 'text/plain'; //tried: 'text/plain', 'text/xml', 'application/json', and others

               TUniGuiSession(Sender).AResponse.ContentText := xStrData;

               TUniGuiSession(Sender).AResponse.WriteContent;

 

               { Up here i got troubles }

 

               //Don't show de login form

               Handled := True;

          end;

     end;

end;

 

I tried other approach too, but still nothing:

 

procedure TUniMainModule.UniGUIMainModuleBeforeLogin(Sender: TObject; var Handled: Boolean);

var

   xAuthKey, xUserInfo, xStrData : string;

begin

 

     Handled := False;

 

     //Try to get authentication info

     xAuthKey := (Sender as TUniGUISession).UniApplication.Parameters.Values['odata'];

     if not xAuthKey.IsEmpty then

     begin

          //Validate authentication info and gets de user info

          xUserInfo := GetUserInfo(xAuthKey);

          if not xUserInfo.IsEmpty then

          begin

               //GetStrData returns information of database based on user data

                 //It can be a JSon, a XML or OData, or even, a simple text

               xStrData := GetStrData(xUserInfo);

 

               { Down here i got troubles }

 

               //Here I got a ajax erro message dialog, with the content of xStrData (Even with the ExecCode param True)

               TUniGuiSession(Sender).SendResponse(xStrData, False);

 

               { Up here i got troubles }

 

               //Don't show de login form

               Handled := True;

          end;

     end;

end;

 

It must be something simple, but I can not do it. I searched a lot in the forums and did not find anything like what I want to do.

 

Thank you in advance.

URLSample.png

Link to comment
Share on other sites

Maybe you need use UniServerModule -> OnHTTPCommand event for this ?:

procedure TUniServerModule.UniGUIServerModuleHTTPCommand(
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo;
  var Handled: Boolean);
begin
  
end;
Link to comment
Share on other sites

Actually by ServerModule's "UniGUIServerModuleHTTPCommand" event I was able to provide static content. But dynamic, which is the case I need I did not get.

 

It's a little tricky to explain, but in my case, the data I'm going to provide is not in the database where my server application are installed.

 

My application is a kind of "bridge" to where the data really is.

 

The data is on my clients' servers (where I use authkey to know who to get the data from).

 

And I do not access the data using a common database connection. I use a messaging traffic component, developed by my company.

 

From what I understand (please correct me if i am wrong) the ServerModule instance is unique to the whole application right? And everything I implement in it I must be aware that it will be shared in every session and I should always perform concurrency validations, right?

 

Anyway, I also could not use "UniGUIServerModuleHTTPCommand" event either.

 

Inside the "UniGUIMainModuleBeforeLogin" container of MainModule, I already have all the necessary validation to establish the connection with the data of the respective user, including the connection component that I use is already available, I do not need to create it and establish the connection in every request (which requires a significant cost).

 

And because the MainModule is per session, it's easy to manage the objects I use to access the data. Since I have no competition between sessions of several different users.

Link to comment
Share on other sites

  • Administrators

Hi,

 

What you are doing here is creating your own HTTP handler for specific urls. In this case you must use UniGUIServerModuleHTTPCommand event. uniGUI session system is too heavy for such a simple task.

Just capture the parameter from HTTP request and perform your task. If you need using some Data components such as Connections and Queries then put them on a Free DataModule which you create and destroy when you serve the request.

 DM := TMyDM.Create(nil);
 try
   // use DM here
 finally
   DM.Free;
 end;

Your task will be executed once, so you don't need a persistent session here. A session is reused by multiple calls from same user while your requests are random and they are not bound to any specific session.

Link to comment
Share on other sites

  • 2 months later...

I have the same problem... ISAPI dont catch the URI for example.


 


My website work's fine localhost:8077/api for example


But when convert into ISAPI, i cant call "api" 


give-me error


 

 

 

My code:

 



procedure TUniServerModule.UniGUIServerModuleHTTPCommand(
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo;
var Handled: Boolean);
var
field_name,nomeDaColuna,valorDaColuna : String;
I: Integer;
Resultado:String;
begin
if (ARequestInfo.Params.Values['funcao']<>'sibsref-todos') and
(ARequestInfo.Params.Values['funcao']<>'sibsref-lista') and
(ARequestInfo.Params.Values['funcao']<>'sibsref-insere') then exit;


if ARequestInfo.Params.Values['funcao']='sibsref-todos' then
begin
//showmessage('1');
AResponseInfo.ContentType := 'text/html';

if ARequestInfo.Params.Count<>3 then
begin
AResponseInfo.ContentText:='error 401';
exit;
end;

ZQueryAPI.Close;
ZQueryAPI.SQL.Clear;
ZQueryAPI.SQL.Text :='select sibs_meps1_id as id,sibs_meps1_meps0id as meps0id, sibs_meps1_dthn as data '+
',sibs_meps1_ref as referencia, sibs_meps1_psc as valor'+
' from sibs_meps1 where dataficheiro>=:Data1 and dataficheiro<=:data2';
ZQueryAPI.ParamByName('Data1').AsString:=copy(ARequestInfo.Params.Values['datainicio'],1,10);
ZQueryAPI.ParamByName('Data2').AsString:=copy(ARequestInfo.Params.Values['datafim'],1,10);
ZQueryAPI.Active := True;
ZQueryAPI.First;
end;

if ARequestInfo.Params.ValueFromIndex[0]='sibsref-lista' then
begin
AResponseInfo.ContentType := 'text/html';
if ARequestInfo.Params.Count<>2 then
begin
AResponseInfo.ContentText:='error 402';
exit;
end;

ZQueryAPI.Close;
ZQueryAPI.SQL.Clear;
ZQueryAPI.SQL.Text :='select sibs_meps1_id as id,sibs_meps1_meps0id as meps0id, sibs_meps1_dthn as data '+
',sibs_meps1_ref as referencia, sibs_meps1_psc as valor'+
' from sibs_meps1 where sibs_meps1_ref=:Data1';
ZQueryAPI.ParamByName('Data1').AsString:=copy(ARequestInfo.Params.Values['ref'],1,10);
ZQueryAPI.Active := True;
ZQueryAPI.First;
end;

if ARequestInfo.Params.ValueFromIndex[0]='sibsref-insere' then
begin
AResponseInfo.ContentType := 'text/html';
//showmessage('3');
if ARequestInfo.Params.Count<>8 then
begin
AResponseInfo.ContentText:='error 404';
exit;
end;

ZQueryAPI.Close;
ZQueryAPI.SQL.Clear;
ZQueryAPI.SQL.Text :='DELETE FROM sibs_entidades WHERE CODIGO=:CODIGO';
ZQueryAPI.ParamByName('CODIGO').AsString:=ARequestInfo.Params.Values['codigo'];
ZQueryAPI.ExecSQL;

ZQueryAPI.Close;
ZQueryAPI.SQL.Clear;
ZQueryAPI.SQL.Text :='INSERT INTO sibs_entidades (CODIGO,TELEMOVEL,EMAIL,NOME,APELIDO,ENTTRAB,CARTAO,REFERENCIA) '+
'VALUES (:CODIGO,:TELEMOVEL,:EMAIL,:NOME,:APELIDO,:ENTTRAB,:CARTAO,:REFERENCIA)';
ZQueryAPI.ParamByName('CODIGO').AsString:=ARequestInfo.Params.Values['codigo'];
ZQueryAPI.ParamByName('TELEMOVEL').AsString:=ARequestInfo.Params.Values['telemovel'];
ZQueryAPI.ParamByName('EMAIL').AsString:=ARequestInfo.Params.Values['email'];
ZQueryAPI.ParamByName('NOME').AsString:=ARequestInfo.Params.Values['nome'];
ZQueryAPI.ParamByName('APELIDO').AsString:=ARequestInfo.Params.Values['apelido'];
ZQueryAPI.ParamByName('ENTTRAB').AsString:=ARequestInfo.Params.Values['EntTrab'];
ZQueryAPI.ParamByName('CARTAO').AsString:=ARequestInfo.Params.Values['cartao'];
ZQueryAPI.ParamByName('REFERENCIA').AsString:=ReferenciaMultibanco(ARequestInfo.Params.ValueFromIndex[1],15);
ZQueryAPI.ExecSQL;

ZQueryAPI.Close;
ZQueryAPI.SQL.Clear;
ZQueryAPI.SQL.Text :='select CODIGO,REFERENCIA from sibs_entidades where codigo=:codigo';
ZQueryAPI.ParamByName('codigo').AsString:=ARequestInfo.Params.ValueFromIndex[1];
ZQueryAPI.Active := True;
ZQueryAPI.First;

end;

if ZQueryAPI.IsEmpty then exit;

Resultado := '[';
while (not ZQueryAPI.EOF) do
begin

Resultado := Resultado+'{';
for I := 0 to ZQueryAPI.FieldDefs.Count-1 do
begin
nomeDaColuna := ZQueryAPI.FieldDefs[I].Name;
valorDaColuna := ZQueryAPI.FieldByName(nomeDaColuna).AsString;
Resultado := Resultado+'"'+nomeDaColuna+'":"'+valorDaColuna+'",';
end;
Delete(Resultado, Length(Resultado), 1);
Resultado := Resultado+'},';

ZQueryAPI.Next;
end;
ZQueryAPI.Refresh;

Delete(Resultado, Length(Resultado), 1);
Resultado := Resultado+']';

AResponseInfo.ContentText:=Resultado;
//AResponseInfo.WriteContent;

Handled := true;

//else
// Handled := False;

end;

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...