[fpc-pascal] TJSONRPCModule: access the request from an handler.

Michael Van Canneyt michael at freepascal.org
Fri Sep 9 15:50:50 CEST 2022



On Fri, 9 Sep 2022, Luca Olivetti via fpc-pascal wrote:

> Hello,
>
> I added a "Web JSON-RPC Module" to my project, I dropped on it a 
> TJSONPCHandler.
>
> I find no way to access the request (I want to check the RemoteAddress):
>
> 1) the module has a "Request" field but it is always nil (either in 
> DataModuleCreate, which isn't called with every request so it's unusable 
> anyway, in BeforeExecute and in Execute, I didn't check AfterExecute but it 
> would be too late).

This is fixed in trunk: Request is always available.

>
> 2) the JSONRPCHandler has no way to access the request (or I couldn't find 
> it).
>
> I guess that, instead of calling the RegisterHTTPModule of the module, I 
> should register the route in my webserver class (with 
> httprouter.RegisterRoute), but what should I do in the handler to delegate 
> the handling to the JSONRPCModule?

The solution is much more simple.

Override HandleRequest() in the module.
Save the request in a variable and call inherited.

I have code that needs to work with 3.2.2 and I do this:

---
procedure TUserRPCModule.HandleRequest(ARequest: TRequest; AResponse: TResponse);

Var
   C : String;

begin
   FRequest:=aRequest;
   FResponse:=aResponse;
   try
     C:=FRequest.Content;
     C:=C+C;
     inherited HandleRequest(ARequest, AResponse);
   finally
     FRequest:=Nil;
     FResponse:=Nil;
   end;
end;
---


Michael.


More information about the fpc-pascal mailing list