Access Request Header in Delphi XE3 DataSnap Serve

2019-02-07 08:52发布

问题:

I am implementing a REST server API in Delphi XE3 (first time using Delphi in about a decade so am a bit rusty). Currently it is using Indy server for debug purposes, but eventually it will be an ISAPI dll.

Now I have implemented a number of TDSServerClass classes and want to access the request header within the class methods. So for example when the user requests mysite.com/datasnap/rest/foo/bar I want to be able to read the header within the foo class method called bar. Is this possible?

If not, is it possible to create a global filter of incoming requests before they get to the REST class method? I need to check the API key and user authentication on incoming requests and not sure the best way to implement. Thanks.

回答1:

I don't know if anything changed in XE3, but in XE2 you can do the following:

uses
  Web.HTTPApp,
  Datasnap.DSHTTPWebBroker;

function TServerMethods1.EchoString(Value: string): string;
var
  Module: TWebModule;
begin
  Module := GetDataSnapWebModule;
  Result := Module.Request.RemoteIP + ': ' + Value;
end;