0

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.

Jonathan Wareham
  • 3,357
  • 7
  • 46
  • 82

1 Answers1

1

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.
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • Hi, many thanks for the response. Forgive me but can you just confirm the code you posted is to be placed in the Datasnap.DSClientRest unit, or can it just be added as a new unit in my project? – Jonathan Wareham Jun 15 '12 at 07:23
  • Hi, sorry I was not clear enough. Put the code in any unit of your own. I wrote this code specifically to avoid modifying the original Datasnap source code. Otherwise you could simply fix it in `TDSRestRequest.GetHTTP` very easily. – Ondrej Kelle Jun 15 '12 at 07:30
  • Fantastic, I don't understand exactly how it works but it works! Many thanks. – Jonathan Wareham Jun 15 '12 at 08:43