Recent

Author Topic: [Solved]cors issue  (Read 408 times)

Packs

  • Sr. Member
  • ****
  • Posts: 368
[Solved]cors issue
« on: September 20, 2024, 01:06:58 pm »
Code: Pascal  [Select][+][-]
  1. procedure old_jsonResponse(var res: TResponse; Data: string;StatusCode : Integer);
  2. var
  3.   Origin: string;
  4. begin
  5.  
  6.   try
  7.  
  8.     res.SetFieldByName('Access-Control-Allow-Origin', '*');
  9.     res.SetFieldByName('Access-Control-Max-Age', '600');
  10.     res.SetFieldByName('Access-Control-Allow-Methods',
  11.       'POST, GET, OPTIONS, PUT, DELETE');
  12.     res.SetFieldByName('Access-Control-Allow-Headers',
  13.       'Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization,responsetype');
  14.  
  15.  
  16.     res.Allow := '*';
  17.     res.Age := '600';
  18.     res.Method := 'POST';
  19.  
  20.     res.Content := Data.Trim;
  21.     res.Code := StatusCode;
  22.    
  23.     res.SendResponse;
  24.  
  25.   except
  26.     on e: Exception do
  27.     begin
  28.       res.Code := 400;
  29.       WriteLn('my response  !' + e.message);
  30.        res.SendResponse;
  31.  
  32.     end;
  33.   end;
  34. end;
  35.  
  36.  

in the above code I am getting cors issue
« Last Edit: October 02, 2024, 07:41:11 pm by Packs »

Packs

  • Sr. Member
  • ****
  • Posts: 368
[solved]Re: cors issue
« Reply #1 on: September 20, 2024, 08:34:01 pm »
How to handle cors issue in httpserver application in free pascal
« Last Edit: September 29, 2024, 09:05:04 pm by Prakash »

Packs

  • Sr. Member
  • ****
  • Posts: 368
Re: cors issue
« Reply #2 on: September 20, 2024, 08:50:28 pm »
location /my-callback-url-path {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' 'https://stagegateway.eko.in';
        add_header 'Access-Control-Allow-Methods' 'POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Content-Type,Authorization';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' 'https://stagegateway.eko.in';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Content-Type,Authorization';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
}


How to implement this in free pascal with httpserver for rest api

 

TinyPortal © 2005-2018