Marc Guillot Marc Guillot - 6 months ago 75
HTTP Question

Add a Custom Header on a TISAPIRequest (Delphi 10.1 Datasnap Server)

Do you know how to manually add a custom header on a TISAPIRequest ?.

This class (or the most generic TWebRequest) doesn't expose a RawHeaders property to allow adding new customized Headers when needed.

PS: I have a dirty solution for when my WebRequest is a TIdHTTPAppRequest (Datasnap standalone server), because then I can create a Helper Class to access to its private FRequestInfo property, and from there gain access to a RawHeaders, which I can use to add a new header. But I only use standalone servers for development and testing, the production environment must run on IIS servers.

TIdHTTPAppRequestHelper = class helper for TIdHTTPAppRequest
function GetRequestInfo: TIdEntityHeaderInfo;


function TIdHTTPAppRequestHelper.GetRequestInfo: TIdEntityHeaderInfo;
Result := FRequestInfo;

procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var Token: string;

Token := Request.Query;

if Copy(Token, 1, 10) = 'dssession=' then begin
if Request is TIdHTTPAppRequest then begin
TIdHTTPAppRequest(Request).GetRequestInfo.RawHeaders.AddValue('Pragma', Token);

if FServerFunctionInvokerAction <> nil then
FServerFunctionInvokerAction.Enabled := AllowServerFunctionInvoker;

It is possible to write a similar code to get the same result (to add a custom header to your WebRequest) when the WebRequest is a TISAPIRequest instead of THTTPAppRequest ?.

Thank you.


TISAPIRequest has a public ECB property, which returns a pointer to the ISAPI EXTENSION_CONTROL_BLOCK structure that represents the underlying request data. However, the ECB does not allow you to alter the request headers in any way, only read values from them. You can set custom response headers via the ECB, though.

The only way I can find to add/modify custom request header values in ISAPI is to write an ISAPI Filter DLL, which is outside the scope of TISAPIRequest handling. Inside the DLL's exported HttpFilterProc() function, the SF_NOTIFY_PREPROC_HEADERS notification provides an HTTP_FILTER_PREPROC_HEADERS structure that contains pointers to AddHeader() and SetHeader() functions for adding/modifying request header values.