2

There has been a change to the HTTPRIO.HTTPWebNode.OnBeforePost event in Delphi 10.3.

Before Delphi 10.3, the event handler was defined this way, and it worked perfectly:

procedure TForm1.HTTPRIO1HTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp;
  Data: Pointer);
var
  auth: String;
begin
  auth := 'Authorization: Basic ' + IdEncoderMIME1.EncodeString('user:password');
  HttpAddRequestHeaders(Data, PChar(auth), Length(auth), HTTP_ADDREQ_FLAG_ADD);
end;

In Delphi 10.3, the Data parameter is gone, instead a THTTPClient is given, and I have no idea how to implement Basic authentication with it:

procedure TForm1.HTTPRIO1HTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp;
  Client: THTTPClient);
var
  auth: String;
begin
  auth := 'Authorization: Basic ' + IdEncoderMIME1.EncodeString('user:password');
  ???
end;

Any hints?

mjn
  • 36,362
  • 28
  • 176
  • 378
ramses
  • 49
  • 1
  • 11

2 Answers2

7

Try using the request's Username and Password properties, eg:

HTTPReqResp.UserName := 'user';
HTTPReqResp.Password := 'password';

If that does not work, try using the client's CustomHeaders property instead, eg:

Client.CustomHeaders['Authorization'] := 'Basic ' + IdEncoderMIME1.EncodeString('user:password');
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2nd proposition works! 1st unfortunatelly not. Thanks! – ramses Feb 22 '19 at 20:31
  • dont forget to enable basic auth – Adriaan Feb 23 '19 at 08:40
  • @AdriaanBoshoff hi, I have the same problem. The secons solution works, but the 1st one seems more elegant. You've mentioned enabling basic auth. Where can I enable this? I've explored components (TNetHTTPClient and TNetHTTPRequest), but haven't found an option. From serverside it's probably enabled, because I can use other REST client to access it via basic auth. – Michal Vašut Jul 08 '19 at 15:57
  • Look at the properties. There's one called `BasicAuthentication`. Set that to true. – Adriaan Jul 09 '19 at 05:22
1

Using THttpClient the below worked for me in 10.3.2 (I couldn't find the lib for IdEncoderMIME1). I was able to post to an api using basic authentication.

HTTP.CustomHeaders['Authorization'] := 'Basic ' + TNetencoding.Base64.Encode('user:pass');

  • 1
    "*I couldn't find the lib for IdEncoderMIME1*" - the `TIdEncoderMIME` class is part of Indy, which comes pre-installed with the IDE. – Remy Lebeau Jun 27 '23 at 14:45