Recent

Author Topic: cors issue with preflight request  (Read 269 times)

Prakash

  • New Member
  • *
  • Posts: 49
cors issue with preflight request
« on: November 08, 2022, 07:24:05 am »
{$mode objfpc}{$H+}
 
uses
  {$ifdef UNIX}
  cthreads, cmem,
  {$endif}     
  fphttpapp, httpdefs, httproute, fpjson, jsonparser;
 
procedure route1(aReq: TRequest; aResp: TResponse);
var
  s:string;
  D,E : TJSONData;
begin
   writeln(areq.Content);
   D:=GetJSON(areq.Content);
   E:=d.FindPath('title');
   s:=E.AsString;
//   aResp.SetStatus(200);
   aResp.Content:='<html><body><h1>Route 1 respone:'+s+'</h1></body></html>';
end;
 
procedure route2(aReq: TRequest; aResp: TResponse);
begin
  aResp.Content:='<html><body><h1>Route 2</h1></body></html>';
end;
 
begin
  HTTPRouter.RegisterRoute('/', @route1);
  HTTPRouter.RegisterRoute('/2', @route2);
  Application.Port := 1999;
  Application.Threaded := true;
  Application.Initialize;
  Application.Run;
end.             
 

when I run this code it is working in postman not working in browser .

I am getting cors isssue .

browse send the preflight request with "options" request .

D:=GetJSON(areq.Content); the content is nil.


getting CORS issue .

PascalDragon

  • Hero Member
  • *****
  • Posts: 4872
  • Compiler Developer
Re: cors issue with preflight request
« Reply #1 on: November 08, 2022, 07:37:46 am »
Please use [code=pascal][/code] tags so that the code is easier to see and isn't mangled by the forum software.

Prakash

  • New Member
  • *
  • Posts: 49
Re: cors issue with preflight request
« Reply #2 on: November 08, 2022, 09:01:49 am »
it is solved


  HTTPRouter.RegisterRoute('/2', rmPost,@route2);

if we don't define method name browser will call preflight .

it we define method name it will be called on specified method only .


 

TinyPortal © 2005-2018