adding Access-Control-Allow-Origin header - Printable Version +- Atozed Forums (https://www.atozed.com/forums) +-- Forum: Indy (https://www.atozed.com/forums/forum-8.html) +--- Forum: Indy General Discussion (https://www.atozed.com/forums/forum-9.html) +--- Thread: adding Access-Control-Allow-Origin header (/thread-2845.html) |
adding Access-Control-Allow-Origin header - Boba TC - 08-20-2022 Dear All; with TIdHTTPServer, how do I add the "Access-Control-Allow-Origin" header? My intuitive move with VCL was: Code: TIdHTTPServer *IdHTTPServer; Code: <!DOCTYPE HTML> BTW, if I insert my URL "http://127.12.34.56:7890/qwe" into the browser's address bar and press Enter - everyone is happy even if the server does not add an extra header. TIA/Boba RE: adding Access-Control-Allow-Origin header - rlebeau - 08-22-2022 (08-20-2022, 04:37 AM)Boba TC Wrote: with TIdHTTPServer, how do I add the "Access-Control-Allow-Origin" header? "Access-Control-Allow-Origin" is a RESPONSE header, but you are adding it to the client's REQUEST headers instead. The server's OnHeadersAvailable event is meant for notifying your server code when the client's REQUEST headers have been received, so your server code can make a decision about whether to accept the request before the request's body is received and an OnCommand... event is fired to process the request. (08-20-2022, 04:37 AM)Boba TC Wrote: My intuitive move with VCL was: Unfortunately, your intuition was incorrect. The correct way to handle this situation is to add the "Access-Control-Allow-Origin" header to the AResponseInfo object provided in the OnCommand... events, for example: Code: void __fastcall TForm1::IdHTTPServerCommandGet(TIdContext *AContext, (08-20-2022, 04:37 AM)Boba TC Wrote: but when I test it with simple HTML in web browser: Because it is looking for the header in the server's response, which is not where you are putting it. RE: adding Access-Control-Allow-Origin header - Boba TC - 08-29-2022 Many thanks, Remy; will test it soon. Works as expected! Sorry for the late reply. .Boba. |