Basic authentication

I need a example of a Get method using basic authentication for a Rest webservice.

In basic authentication you set the HTTP request header as for example:

Authorization: Basic AXVubzpwQDU1dzByYM==

where the value after basic is the base64-encoded(non-encrypted) string username: password.

I have no luck with the headers.

procedure TForm1.Test;
headers: TCoreCloudHeaders;
AddHeader(headers,'Authorization', 'Basic UElFVDoxMjM0');
WebRESTClient1.HttpsGet(BaseURL + 'ListCompanies', headers);

The webserver fails before it gets this header.

The following header is send by TMS, from what I can see in the webserver.

Connection: keep-alive
Accept: /
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization
Origin: http://localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
Sec-Fetch-Dest: empty
Referer: http://localhost:8000/
Accept-Encoding: gzip, deflate, br
Accept-Language: nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7

This is sent by Delphi it self, when I execute a get from the RestDebugger.

Connection: Keep-Alive
Accept: application/json, text/plain; q=0.9, text/html;q=0.8,
Accept-Charset: utf-8, *;q=0.8
Authorization: Basic UElFVDoxMjM0
User-Agent: Embarcadero RESTClient/1.0

What am I doing wrong?

What exact error is in the browser console?

That is the typical CORS issue, explained in this FAQ

The link does not exist.

I can open this link without any issue.

I think I have a solution

Webcore sents a method "OPTIONS" before the get.
The web server does not expect this method request and sent a 401 back.

I patched the source of the web server by sending a 204 back on the "OPTIONS" method.
After that everything works.

So nothing to do with CORS.

Hi Piet,

sorry, but Bruno is right. This is all about CORS.

The OPTIONS method is a so called preflight request. The expected response to that request is 204.

You can read about this here: click me


That makes sense.
I am learning a lot, hopefully I can now concentrate on Webcore.