Jump to content

api rest in an unigui project


gerardocrisci

Recommended Posts

  • 7 months later...

You should be able to add an IdHttpServer in the UniServerModule,

and process the http requests that way, but on a different port than 

the server application of course.

 

This demands that the application is loaded, which will not happen

until the first mainmodule request is made, as far as I have experienced.

Link to comment
Share on other sites

As delphidude suggested.

 

But you can also use the OnHTTPCommand event on the ServerModule. Just be aware that this is not associated with a session. Just do a search in the forum for "OnHTTPCommand". Below is an example.

procedure TUniServerModule.UniGUIServerModuleHTTPCommand(ARequestInfo: TIdHTTPRequestInfo;
  AResponseInfo: TIdHTTPResponseInfo; var Handled: Boolean);
var
  dm : TdmReportData;

begin
  if ARequestInfo.URI='/report/finsummary' then
  begin
    dm := TdmReportData.Create(nil);
    try
      dm.qryReport.Connection := dm.FDConnection;
      AResponseInfo.ContentText := dm.GenerateReportPDF(repFinSummary, '') + ' header [user] = ' + ARequestInfo.RawHeaders.Values['user'];
      AResponseInfo.ResponseNo := 200;
      AResponseInfo.WriteContent;
      Handled := True;
    finally
      dm.Free;
    end;
  end;
  if ARequestInfo.URI='/report/sleep' then
  begin
    Sleep(10000);
    AResponseInfo.ResponseNo := 200;
    AResponseInfo.ContentText := 'Finished sleeping!';
    AResponseInfo.WriteContent;
    Handled := True;
  end;
end;
  • Like 1
  • Upvote 1
Link to comment
Share on other sites

  • 5 months later...

@GerhardVI just tried what you suggested and it works fine as long as I do not add any header information.

If I enter for example the following cURL command: curl "http://localhost:8077/report/sleep" -H "Authorization: ffff"

I do not even enter the event handler and get the response curl: (52) Empty reply from server

Do you have any idea, how to pass header information to the UniGUIServerModule and extract them?

Link to comment
Share on other sites

Ok, my fault - I used a forbidden Keyword in my header (Authorization).

In case of using anything else it works fine. Header values can be accessed easily. For example if I define a key "test" with a value "88" the following call will set "s" to "88".

s := ARequestInfo.RawHeaders.Values['test'];

 

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...