Jump to content
uniGUI Discussion Forums
gerardocrisci

api rest in an unigui project

Recommended Posts

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.

Share this post


Link to post
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;
  • Upvote 1

Share this post


Link to post
Share on other sites

@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?

Share this post


Link to post
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'];

 

Share this post


Link to post
Share on other sites

mhmda : do you use your API as data provider for your unigui app or do you use firedac component with direct acces to db?

Share this post


Link to post
Share on other sites

We use  something like ORM between unigui and DB when it comes to CRUD operations. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×