Atozed Forums
Basic Authentication Error - Printable Version

+- Atozed Forums (https://www.atozed.com/forums)
+-- Forum: Open Source (https://www.atozed.com/forums/forum-19.html)
+--- Forum: Indy (https://www.atozed.com/forums/forum-9.html)
+--- Thread: Basic Authentication Error (/thread-3678.html)



Basic Authentication Error - staff@ergosoft.it - 12-04-2023

Hi,

I have a problem on a API REST call.

I always get the authentication error: HTTP/1.1 401 Unauthorized

this is my code in delphi 11.3

thanks
Alessandro Romano
//-----------------------------------------------------------
var
lHTTP: TIdHTTP;
SSL : TIdSSLIOHandlerSocketOpenSSL;
Comando, RisultatoPost : Ansistring;
lPostData: TStringStream;
base64: TBase64Encoding;
myUser, MyPass : string;
begin
  Comando := 'https://apirest... etc';
  base64 := TBase64Encoding.Create(0);

  myUser := 'myuser';
  MyPass := 'mypass';

  lPostData := TStringStream.Create('', TEncoding.UTF8);
  with TJSONObject.Create do
  try
      AddPair('Authorization', 'Basic ' + base64.Encode(myUser + ':' + MyPass));
      lPostData.Writestring(ToJSON);
  finally
    Free;
  end;

  lPostData.Position := 0;
  try
    lHTTP := TIdHTTP.Create;
    SSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
    SSL.SSLOptions.Method := sslvTLSv1_2;

        lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
        lHTTP.IOHandler := SSL;
        lHTTP.HandleRedirects := True;
        lHTTP.Request.CustomHeaders.Clear;
        lHTTP.Request.CustomHeaders.FoldLines := False;
        lHTTP.Request.Accept := 'application/json';

        //lHTTP.Request.ContentType := 'application/json';
        //lHTTP.Request.CharSet := 'UTF-8';

        RisultatoPost := lHTTP.Post(Comando, lPostData);
         

    finally
      SSL.Free;
      lHTTP.Free;
      lPostData.Free;
      base64.Free;
  end;


RE: Basic Authentication Error - rlebeau - 12-04-2023

(12-04-2023, 11:20 AM)staff@ergosoft.it Wrote: I have a problem on a API REST call.

I always get the authentication error: HTTP/1.1 401 Unauthorized

TIdHTTP has built-in support for BASIC authentication at the HTTP layer. Are you sure you should be sending your credentials in the POST body? Do you get the same error if you use the standard HTTP Authorization header instead? TIdHTTP has properties for that purpose:

Code:
lHTTP.Request.BasicAuthentication := True;
lHTTP.Request.Username := myUser;
lHTTP.Request.Password := MyPass;



RE: Basic Authentication Error - staff@ergosoft.it - 12-05-2023

(12-04-2023, 05:48 PM)rlebeau Wrote:
(12-04-2023, 11:20 AM)staff@ergosoft.it Wrote: I have a problem on a API REST call.

I always get the authentication error: HTTP/1.1 401 Unauthorized

TIdHTTP has built-in support for BASIC authentication at the HTTP layer.  Are you sure you should be sending your credentials in the POST body?  Do you get the same error if you use the standard HTTP Authorization header instead? TIdHTTP has properties for that purpose:

Code:
lHTTP.Request.BasicAuthentication := True;
lHTTP.Request.Username := myUser;
lHTTP.Request.Password := MyPass;

Thank you ! works


Alessandro Romano