Jump to content
Yaron

How do I get the client's IP address in a Server.Resources function?

Recommended Posts

Hi @Andrea Magni

To improve security and statistics, I need access to the client's IP address when my function triggers in server.resources:

 

  [Path('projectX')]
  TProjectXServicesResource = class
  protected
  public
    [GET, Produces(TMediaType.TEXT_HTML)]
    function xHome([QueryParam] lang : String) : String;

    [POST, Consumes(TMediaType.MULTIPART_FORM_DATA), Produces(TMediaType.TEXT_HTML)]
    function xAction([FormParams] AParams: TArray<TFormParam>): String;
  end;

function TProjectXServicesResource.xHome([QueryParam] lang : String) : String;
begin
  // Need access to the client's IP here
end;

function TProjectXServicesResource.xAction([FormParams] AParams: TArray<TFormParam>) : String;
begin
  // Need access to the client's IP here
end;

How can I obtain the client IP?

Share this post


Link to post

If anyone else is wondering how it's done, you need to add "[Context] marsRequest: TWebRequest;" and then the client's IP is available in "marsRequest.RemoteIP".

  [Path('projectX')]
  TProjectXServicesResource = class
  protected
    [Context] marsRequest: TWebRequest;
  public
    [GET, Produces(TMediaType.TEXT_HTML)]
    function xHome([QueryParam] lang : String) : String;

    [POST, Consumes(TMediaType.MULTIPART_FORM_DATA), Produces(TMediaType.TEXT_HTML)]
    function xAction([FormParams] AParams: TArray<TFormParam>): String;
  end;

function TProjectXServicesResource.xHome([QueryParam] lang : String) : String;
begin
  ShowMessage(marsRequest.RemoteIP);
end;

function TProjectXServicesResource.xAction([FormParams] AParams: TArray<TFormParam>) : String;
begin
  ShowMessage(marsRequest.RemoteIP);
end;

 

Share this post


Link to post
On 12/22/2019 at 12:09 PM, Yaron said:

If anyone else is wondering how it's done, you need to add "[Context] marsRequest: TWebRequest;" and then the client's IP is available in "marsRequest.RemoteIP".


  [Path('projectX')]
  TProjectXServicesResource = class
  protected
    [Context] marsRequest: TWebRequest;
  public
    [GET, Produces(TMediaType.TEXT_HTML)]
    function xHome([QueryParam] lang : String) : String;

    [POST, Consumes(TMediaType.MULTIPART_FORM_DATA), Produces(TMediaType.TEXT_HTML)]
    function xAction([FormParams] AParams: TArray<TFormParam>): String;
  end;

function TProjectXServicesResource.xHome([QueryParam] lang : String) : String;
begin
  ShowMessage(marsRequest.RemoteIP);
end;

function TProjectXServicesResource.xAction([FormParams] AParams: TArray<TFormParam>) : String;
begin
  ShowMessage(marsRequest.RemoteIP);
end;

 

Hi,

I am following down this path - but no success:
- marsRequest stays nil
- Also ading [HeaderParam] and  [QueryParam] stay empty.

What I am looking for is
having the RequestURL to create an auto refresh status page which autorefreshs after a short time.
Don't want to set URL as constant string...

Any hints how to retrieve the Request URL simplest way
(Just scrolling through Demo Code, but not seeing the trees instead of the wood...)

Kind Greetings,

    Marcovaldo

Share this post


Link to post

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
×