5

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.

Joel
  • 15,654
  • 5
  • 37
  • 60
  • Are you using the built-in authentication/authorization object? – jachguate Dec 28 '12 at 01:44
  • No. API key and user token are passed in the request header. I need to read them. – Joel Dec 28 '12 at 02:24
  • ps. Authentication should not be done at the server level as only some class methods require user authentication (token), and others do not (all methods require a valid API key). – Joel Dec 28 '12 at 02:44

1 Answers1

2

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;
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • 1
    So this answers the question of how to access the TWebRequest object from within a server class method. I'm going to mark as correct, but in order to parse the headers I used this solution plus this answer to access the headers: http://stackoverflow.com/questions/8666411/enumerate-twebrequest-http-header-fields. – Joel Dec 30 '12 at 18:10