TXDataWebConnection with Basic Auth

Hello,
with REST-Debugger I can use Auth BASIC and user xyxyxy and password zyzyzyz
and it works fine, I get my right data.
With TXDataWebConnection I dont know how to set the "user" and "password".
Can someone help?
Best Dirk

Hi, you must set the needed headers using OnRequest event, example:

procedure TForm1.XDataWebConnection1Request(Request: TXDataWebConnectionRequest);
begin
  Request.Request.Headers.SetValue('Authorization', 'Basic ' + BasicAuthString);
end;

Where BasicAuthString should comply with Http Basic authentication standards ('userid:passwd' base64 encoded)

HTH,

Many Thanks Farias
try
coder:=TBase64Encoding.Create;
BasicAuthString:=coder.Encode('username:password');
finally
coder.Free;
end;

Args.Request.Headers.SetValue('Authorization', 'Basic ' + BasicAuthString);

But always no connection...

Any other tips?

Best Dirk

Hi. Sorry to hear that.

Are you setting headers on XDataWebConnection OnRequest event ?

Regards,

Hi,
yes:

procedure TForm1.XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
var
  BasicAuthString: string;
  coder: TBase64Encoding;

begin
  try
   coder:=TBase64Encoding.Create;
   BasicAuthString:=coder.Encode('xyxyxy:zyzyzy');
  finally
    coder.Free;
  end;

  Args.Request.Headers.SetValue('Authorization', 'Basic ' + BasicAuthString);
end;

And with RestDebugger it works:


I'm out of ideas. Do you get any error messages or just http 401 response code? Can you debug your server side?

Regards,

Please specify what is "does not work". What exact operation are you performing, and what exact result do you get?

Hello,

My code:

unit Unit1;

interface

uses
  System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
  WEBLib.Forms, WEBLib.Dialogs, Vcl.Controls, Vcl.Grids, WEBLib.DBCtrls,
  Data.DB, WEBLib.DB, XData.Web.JsonDataset, XData.Web.Dataset,
  XData.Web.Connection, Vcl.StdCtrls, WEBLib.StdCtrls, WEBLib.ExtCtrls, Vcl.Graphics,
  XData.Web.Client, WEBLib.CDS, System.NetEncoding;

type
  TForm1 = class(TWebForm)
    XDataWebConnection1: TXDataWebConnection;
    XDataWebDataSet1: TXDataWebDataSet;
    XDataWebDataSet1Id: TIntegerField;
    XDataWebDataSet1Name: TStringField;
    XDataWebDataSet1Nummer: TIntegerField;
    XDataWebDataSet1Wer: TStringField;
    XDataWebDataSet1Frage: TStringField;
    XDataWebDataSet1Antwort: TStringField;
    XDataWebDataSet1Antstatus: TIntegerField;
    XDataWebDataSet1Datum: TDateTimeField;
    XDataWebDataSet1Gesendet: TBooleanField;
    XDataWebDataSet1Gesperrt: TBooleanField;
    XDataWebDataSet1Pool: TBooleanField;
    XDataWebDataSet1Hot: TBooleanField;
    XDataWebDataSet1New: TBooleanField;
    XDataWebDataSet1Fragen_id: TIntegerField;
    WebDataSource1: TWebDataSource;
    WebDBGrid1: TWebDBGrid;
    WebDBNavigator1: TWebDBNavigator;
    procedure XDataWebConnection1Connect(Sender: TObject);
    procedure WebFormCreate(Sender: TObject);
    procedure XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WebFormCreate(Sender: TObject);
begin
  //
end;

procedure TForm1.XDataWebConnection1Connect(Sender: TObject);
begin
  XDataWebDataSet1.Load;
end;

procedure TForm1.XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
var
  BasicAuthString: string;
  coder: TBase64Encoding;

begin
  try
   coder:=TBase64Encoding.Create;
   BasicAuthString:=coder.Encode(xyxyxy:zyzyzy');
   //showmessage(BasicAuthString);   //xyzxyzxyzxyzxzy
  finally
    coder.Free;
  end;

  Args.Request.Headers.SetValue('Authorization', 'Basic ' + BasicAuthString);
end;

end.

ERROR:
ERROR
XDataConnectionError: Error connecting to XData server | fMessage::XDataConnectionError: Error connecting to XData server FJSError::Error: XDataConnectionError: Error connecting to XData server fHelpContext::0
at http://localhost:8000/Project1/Project1.js [81384:13]

with connection with another server without Authorization and without htttps the Grid shows the Data

Best Dirk

Hello,
I did some research and found the following.
Calling local with "localhost" does not work
After importing to the server, calling "www" does not work either, without "www" it works ???



Please check this documentation topic and let us know if it helps:

Hello,
I think the problem is with the server code.
What I enter in instead of: 'https://localhost/tms/xdata' is totally irrelevant.
So I think the entry is incorrect:

unit WebModuleUnit1;

interface

uses
  System.SysUtils, System.Classes, Web.HTTPApp,
  Sparkle.HttpServer.Module, Sparkle.HttpServer.Context, Sparkle.Comp.Server,
  XData.Comp.Server, XData.OpenAPI.Service;

type
  TWebModule1 = class(TWebModule)
    procedure WebModule1DefaultHandlerAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  WebModuleClass: TComponentClass = TWebModule1;

implementation

uses
  Sparkle.WebBroker.Server,
  Sparkle.WebBroker.Adapter,
  XData.Server.Module, ConnectionModule, Aurelius.Engine.DatabaseManager,
  Sparkle.Middleware.Cors;

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

var
  Server: TWebBrokerServer;
  Module: TXDataServerModule;

procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  Adapter: IWebBrokerAdapter;
begin
  Adapter:=TWebBrokerAdapter.Create(Request, Response);
  Server.DispatchRequest(Adapter);
end;

initialization

   Server:=TWebBrokerServer.Create;

   TDatabaseManager.Update(TSQLiteConnection.CreateConnection);

   Module:=(TXDataServerModule.Create(
   'https://localhost/tms/xdata', TSQLiteConnection.CreateConnection
   ));
   Module.AddMiddleware(TCorsMiddleware.Create);

   Server.Dispatcher.AddModule(Module);

   RegisterOpenAPIService;

finalization
   Server.Free;

end.


Best Dirk

Have you checked the link I sent you?
What is the exact error detail being displayed in the console related to the request?
It looks like you are using Apache, which you didn't mentioned before. The behavior also depends on the Apache configuration, have you properly configured it to handle both www and non-www requests?