Access Request Header in Delphi XE3 DataSnap Server
Asked Answered
A

1

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.

Afterbirth answered 26/12, 2012 at 22:57 Comment(3)
Are you using the built-in authentication/authorization object?Expedite
No. API key and user token are passed in the request header. I need to read them.Afterbirth
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).Afterbirth
F
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;
Foreordination answered 30/12, 2012 at 15:48 Comment(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: #8666911.Afterbirth

© 2022 - 2024 — McMap. All rights reserved.