Delphi Datasnap client code not getting unauthorized exception

org.springframework.web.client.httpclienterrorexception: 401 unauthorized
sharepoint rest api 401 unauthorized
xhr 401 unauthorized
c# 401 unauthorized
401 - unauthorized: access is denied due to invalid credentials
swagger 401 unauthorized
how to fix 401 unauthorized error in angular
salesforce rest api 401 unauthorized

I'm using Delphi 10.1 Berlin Update 2 Enterprise and the DataSnap client/server REST framework.

If I run the app without debugging and invoke a method the user isn't authorized to invoke, the code runs without any exception and the method returns a null response.

When interactively debugging a call on the client to a DataSnap server method, I get two popup exceptions regarding "unauthorized".

The first bubbles up and is replaced by the second.

The second exception gets "eaten" and the session/connection simply closed and then the method returns a blank result (e.g. a zero if the return type is integer, and an empty string for a string return type).

This is happening in the following section of code near the end of the ExecuteRequest method in the Datasnap.DSClientRest unit:

except
  on E: TDSRestProtocolException do
    LSessionExpired;
end;

Why are these exceptions (e.g. TDSRestProtocolException) not reaching my code?

I kind of think this is new to Update 2, and I remember seeing those exceptions bubble up to my code prior to Update 2.

Attached is a skeleton example (standard example generated by Delphi wizards) that demonstrates the issue - click the button and you get "" instead of "4321" because the user isn't authorized - but no runtime exception.

I'm new to DataSnap, so bear with me :-)

Thanks in advance for helpful responses =)

This is happening due to DSAuthenticationManager1 component added to webmodule of the server and client side is failing to authenticate.

Please go through this to check how to work with authentication Adding Authentication and Authorization

TDSRestConnection does not send User and Password to , When interactively debugging a call on the client to a DataSnap server method, I get two popup exceptions regarding "unauthorized". The first bubbles up and is  HiIve been having problems with REST client calls on Delphi Rio 10.3.1. The problem actually started with Delphi Tokyo 10.2.1. Everything worked perfectly with Delphi Berlin 10.1.2. Not always, but quite often, Im getting a TDSRestProtocolException exception on the client (which is running on win

Well..I'm not sure but try providing username and password to DSRestConnection1 component before the instance of server methods gets created

procedure TClientModule1.TestCon(aUsername, aPassword: string);
var
lServerMethodsClient : TServerMethodsClient;
begin
DSRestConnection1.UserName := aUsername;
DSRestConnection1.Password := aPassword;
lServerMethodsClient:=TServerMethodsClient.Create(DSRestConnection1);
end;

and try to call this functn from ur clientform

procedure TF_ClientForm.Button1Click(Sender: TObject);
begin
ClientModule1.TestCon(EdtUsername.Text, EdtPassword.Text);
end;

401 Unauthorized Error: What It Is and How to Fix It, If the client application is compiled for example in XE7 , works fine sending I use in this project Delphi 10.1 berlin, Firemonky, datasnap (not REST), sql server, is wrong on Android, I'm keep getting exception "HTTP/1.1 401 Unauthorized",​  Delphi 2009 introduced support for DataSnap server methods. If you are not familiar with this feature, here are two articles that describe server methods: DataSnap 2009 Overview , Getting Started with Delphi DataSnap 2009. DataSnap server methods support a variety of parameter and return types.

Maybe a little late but this morning I've had a deep dive into this because, after upgrading from Delphi XE6 to Tokyo 10.2, applications where I used the TDSRestConnection component got broken. Although I supplied the correct username and password, they did not appear in the TDSAuthenticationManager.OnUserAuthenticate event. The 'problem' has to do with the new System.Net.HttpClient implementation.

To make a long story short (or a little bit less long): The client component does not send the credentials until the receiving server demands one by sending a 401 response. After receiving this (properly formatted) response the client looks at de TDSConnection credentials en tries again. At the client side a complete list of urls with credential requirements is maintaned so repetitive calls to the same url go 'smoother'.

I added this code to the server's WebModule (where the TDSRESTWebDispatcher resides) which solved my problems:

procedure TwbmMain.WebModuleBeforeDispatch(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  LAuthorization: string;
begin
  inherited;

  if Request.PathInfo.StartsWith('/datasnap/') then
  begin
    LAuthorization := TNetEncoding.Base64.Decode(Request.Authorization.Replace('Basic ', ''));

    if LAuthorization.IsEmpty then
    begin
      Response.StatusCode := 401;
      Response.WWWAuthenticate := 'Basic';
      Handled := True;
    end;
  end;
end;

Because my applications provides some downloadable items like a logo etc., I limited the check to just those URLs that have anything to do with datasnap.

Hope this is useful to others!

Datasnap Handling Exceptions, Delphi Datasnap client code not getting unauthorized exception. I'm using Delphi 10.1 Berlin Update 2 Enterprise and the DataSnap client/server REST  Hi All, I have been searching around on the forums for an answer and have not found one for this particular problem. I have a datasnap server that I have running from XE4 that has the following piece of code where I raise an exception {code} if not bAvailable then raise Exception.Create('My custom message'); {code} There is a bit more to the code than that but that is the important part on the

[PDF] Delphi 2010 DataSnap, The 401 Unauthorized Error is an HTTP response status code indicating that the request sent by the client could not be authenticated. lead to a solution, such as showing which particular authentication schemes is expected by the server. Plus, Airbrake makes it easy to customize exception parameters,  I think it is not a problem of Delphi DataSnap. It should be a problem of IIS user permissions. Did someone know how to setup IIS user permissions to work? Note: If I add the AppPoolUser to the admin group it will work. But it is not a good idea :-) regards M. Pannier XE Datasnap Client expection with a XE2 DataSnap Server.

Handling TDataSet results in Xcode from Delphi XE2 Datasnap Server, 3 - unit uServerMethodsSRV // like a DataModule in a application Client/Server / here you can create your business rules to acess your data for example. DataSnap is advertised for c++ 2010, but does anyone know if it is actually supposed to work in 2010? The reason I ask is I can't get a datasnap server to work at all. I have succeeded in getting a client to work but the client proxy generator generates buggy code that requires manual changes before it will work.

DataSnap REST server - enable HTTP KeepAlive, Delphi 2010 DataSnap: Your data where you want it, how you want it. DataSnap Server Example . A sample DataSnap server and client will be built, and w The TDSErrorEventObject also includes the Exception that caused the people with data sniffers will not be able to get their hands on your  REST Servers in Delphi XE Using DataSnap Client/server in nature (nothing directly to do with database RDBMS here) Delphi code should clarify the main concepts.

Comments
  • NOTE this is using a Client DataSnap REST module (http), not the simple TCP/IP Client DataSnap module.
  • The article uses a DataSnap Client Module, not the DataSnap Client REST Module that I need to use. Even if I set the username on the client DSRestConnection1 component, the authorization exception still doesn't reach the UI. NOTE! These exceptions are occurring in the library code, but they aren't bubbled up to my code. Authorization checking is occurring, but my UI code isn't seeing the exceptions because they're being eaten in the library code.
  • For clarity, the SQLConnection1 in the "Adding Authentication and Authorization" article works as expected, and the UI sees the unauthorized exception. It's the DSRestConnection1 in my sample project that doesn't see that same exception under the same configuration.
  • The problem isn't with authentication but with authourization :-)
  • I ended up switching to a "DataSnap Server" and "DataSnap Client Module" over TCP/IP. Works beautifully. Note sure why the REST equivalents have this issue, but I've wasted enough time! Thanks, pn098, for taking time to contribute =D