Jump to content
Sign in to follow this  
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

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
Sign in to follow this  
×