I have a Delphi XE2 Win32 app that connects to a REST service using DataSnap HTTP. The HTTP connection uses the default 'User-Agent' header of 'Mozilla/3.0 (compatible; Indy Library)'. I'd like to change this to be something more specific to my app so I can monitor connections on the server from different app editions. I'm using TDSRESTConnection to make the connection - can anybody point me to the object/property I need to work with to set the 'User-Agent'? I've tried using the following :
TDSRESTConnection.HTTP.Request.CustomHeaders.AddValue('User-Agent', 'MyText');
but this didn't make any difference.
Unfortunately, your custom headers are cleared and ignored in TDSRestRequest.GetHTTP
(and TDSRestRequest
is hidden in the implementation of Datasnap.DSClientRest
unit).
Try this workaround:
uses
Datasnap.DSHTTP, IdHTTPHeaderInfo;
const
SUserAgent = 'MyUserAgent';
type
TDSHTTPEx = class(TDSHTTP)
constructor Create(AOwner: TComponent; const AIPImplementationID: string); override;
end;
TDSHTTPSEx = class(TDSHTTPS)
constructor Create(const AIPImplementationID: string); override;
end;
{ TDSHTTPEx }
constructor TDSHTTPEx.Create(AOwner: TComponent; const AIPImplementationID: string);
begin
inherited Create(AOwner, AIPImplementationID);
with Request.GetObject as TIdRequestHeaderInfo do
UserAgent := SUserAgent;
end;
{ TDSHTTPSEx }
constructor TDSHTTPSEx.Create(const AIPImplementationID: string);
begin
inherited Create(AIPImplementationID);
with Request.GetObject as TIdRequestHeaderInfo do
UserAgent := SUserAgent;
end;
initialization
TDSHTTP.UnregisterProtocol('http');
TDSHTTP.RegisterProtocol('http', TDSHTTPEx);
TDSHTTP.UnregisterProtocol('https');
TDSHTTPS.RegisterProtocol('https', TDSHTTPSEx);
finalization
TDSHTTP.UnregisterProtocol('http');
TDSHTTP.UnregisterProtocol('https');
end.