Getting a client ip address in HttpApi Service

Hello.

I’m trying to convert some RODL services to HttpAPI.

In rodl service I could get client ip address like this

public virtual AOperation()
{
    ...
    var ip = (this.ServerChannel as IpHttpServerChannelInfo).RemoteEndPoint.Address;
    ...
}

but in HttpAPI service ServerChannel is always null.
I tried implementing IChannelAwareService, but OnServiceActivated method never invoked.

How can I access ServerChannel of HttpAPI service and get client ip address.

Logged as bugs://D19186.

Hello

Unfortunately it is not possible to retrieve this information in the current implementation of HttpAPI
I have logged an issue to investigate if it is possible to provide this information.

Sorry for the inconvenience

bugs://D19186 was closed as fixed.

As I using delphi, is it fixed also?

How can I get the update if fixed? I need update to which version?

joe

Hi,

in Delphi you can use the OnGetDispatchInfo event.
see more info at the Receiving information about client on server-side topic

As my Project function call is HttpApi, How can I get the client wan ip within the httpapi call ?

joe

Hi,

You can store it in local variable of service or inside the Session

as some httpapi function call no session is needed, can you explain how to store it in a local variable ?

as my understanding , the httpapi ia independent of each call if no session is needed.

joe

Hi,

you can store info about current call in private variable of service.

for example, this code:

  [ROService(__ServiceName, __ServiceID)]
  [RONamespace(fServerDataModule.__RODLLibraryNamespace)]
  [ROStandardClassFactory]
  TNewProjectService = class(TRORemoteDataModule)
    procedure RORemoteDataModuleGetDispatchInfo(const aTransport: IROTransport; const aMessage: IROMessage);
  private
    faddress: string;
  public
    [ROServiceMethod]
    [ROCustom('HttpApiPath', 'test')]
    [ROCustom('HttpApiMethod', 'Get')]
    function NewMethod: string;
  end;

implementation

{%CLASSGROUP 'System.Classes.TPersistent'}
{$R *.dfm}

function TNewProjectService.NewMethod: string;
begin
  Result := faddress;
end;

procedure TNewProjectService.RORemoteDataModuleGetDispatchInfo(const aTransport: IROTransport; const aMessage: IROMessage);
begin
  faddress := (aTransport as IROHTTPTransport).ClientAddress;
end;

will return client address:

Y:\>curl -v -X GET "http://localhost:8099/api/test" -H  "accept: application/json" -H "Authorization: Basic YTpi"
Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying ::1...
* TCP_NODELAY set
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8099 (#0)
> GET /api/test HTTP/1.1
> Host: localhost:8099
> User-Agent: curl/7.56.0
> accept: application/json
> Authorization: Basic YTpi
>
< HTTP/1.1 200 OK
< Connection: close
< Content-Type: application/json; charset=utf-8
< Content-Length: 11
< Date: Tue, 07 Dec 2021 10:21:28 GMT
< WWW-Authenticate: Basic realm="aaaa"
< Accept-Encoding: gzip, identity
<
"127.0.0.1"
* Closing connection 0