I created 'Datasnap Server' by wizard. Client application sends me requests (GET or POST). Before sending GET or POST client sends request (OPTIONS). My Datasnap server does not recognize OPTIONS request.
Is there any possibility to make Datasnap Server recognize and accept OPTIONS request?
Delphi 12 / Datasnap Server / Components: TDSServer, TDSHTTPServer
I often notice issues with CORS (Cross-Origin Resource Sharing) when clients attempt to check the allowed or expected headers using the Access-Control-Allow-Headers.
In my projects, I handle this by utilizing the WebModuleBeforeDispatch event to allow the OPTIONS header. This way, I can respond with the requested Access-Control-Allow-Headers, effectively addressing the CORS requirements.
Here's a sample implementation:
procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
if Request.Method = 'OPTIONS' then
begin
Response.SetCustomHeader('Access-Control-Allow-Origin', '*');
Response.SetCustomHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
if Trim(Request.GetFieldByName('Access-Control-Request-Headers')) <> '' then
begin
Response.SetCustomHeader('Access-Control-Allow-Headers', Request.GetFieldByName('Access-Control-Request-Headers'));
end;
Response.Content := '';
Response.StatusCode := 200;
Handled := True;
end;
end;