WebClientConnection with JWT?


Can I use the WebClientConnection with a server requiring a JWT Access Token?

I've tried accessing my resource but I get a 403 error.


403 means the access was forbidden, so either the JWT was not passed correct or the JWT itself was not correct.
Typically the JWT is passed via the Authentication header in the HTTPS request.

You typically use a TWebHttpRequest via HTTPS and use TWebHttpRequest.Headers where you add the token:

TWebHttpRequest.Headers.Add('Authorization: Bearer' + JWTauthToken);


So does that mean the TWebClientConnection headers can't be amended so I can't use a WebClientConnection with JWT?

That would be a shame.

This isn't exposed at this moment as published property, but indeed it makes sense.
We'll expose this in the next update.

Hi Bruno,

I've updated to

If I add the JWT token the WebClientConnection like this:

      WebClientConnection1.Headers.Add('Authorization: Bearer ' + fAccessToken);

chrome console produces this error:

      Uncaught DOMException: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': '' is not a valid HTTP header field name.

If I add the JWT token to the WebClientConnection as follows:

      WebClientConnection1.Headers.Values['Authorization'] := 'Bearer ' + fAccessToken;

and the chrome console window produces the following error when I set Active=true:

      {fMessage:  "Invalid field size : 0", fHelpContext, 0}

Am I doing anything wrong here?


(Delphi 10.1 Berlin)

Full code:

  WebClientConnection1.URI :=  TAppSettings.GetURL( 'user/list/all' );
  WebClientConnection1.DataNode := 'users';
  WebClientConnection1.Headers.Add('Authorization: Bearer ' + fAccessToken);
  WebClientConnection1.Active := true;

Please set the header via:

WebClientConnection1.Headers.Add('Authorization=Bearer ' + fAccessToken);

Thanks, this worked & I'm getting JSON back.  

I have another problem related to the WebDataset that I'll open a new support ticket for.