480
Views
14
Comments
Solved
REST API set Access-Control-Allow-Origin in response
Question

I need to set Access-Control-Allow-Origin in response for my exposed REST API. I have tried to do it at Outsystems' customize response onResponse action by using the HTTPRequestHandler's AddHeader method. But the result will be having 2 Access-Control-Allow-Origin headers instead which will generate error.

2025-09-25 22-50-38
Hanno
Solution

It sounds like it might be an IIS config.

Can you perhaps see if your IIS is adding/sending any CORS headers?

2025-09-25 22-50-38
Hanno

You could use ListIndex to determine the index of the Access-Control-Allow-Origin header in the Response.Headers list.

If you have the index, you could then set the value of the Access-Control-Allow-Origin header directly by using something similiar to: Response.Headers[index].Value = "[RequiredValue]"

UserImage.jpg
Lavin

Hanno wrote:

You could use ListIndex to determine the index of the Access-Control-Allow-Origin header in the Response.Headers list.

If you have the index, you could then set the value of the Access-Control-Allow-Origin header directly by using something similiar to: Response.Headers[index].Value = "[RequiredValue]"

The Response does not have Response.Headers.  Only ResponseText and ResponseBinary are accessible.


2025-09-25 22-50-38
Hanno

If you add the OnAfterResponse Callbackto the consumed REST API, you should have access to the headers as per the below:

2019-09-30 07-35-56
Aurelio Junior

@Hanno what you mention is for consumed REST APIs, but this question is about an exposed one.

@Lavin you could clone the HTTPRequestHandler extension and change the code to add the header when it doesn't exist or update its value when it does. I was taking a look at the code and it seems to be pretty straight forward. Is this an option for you? Are you familiar with C# code?

2025-09-25 22-50-38
Hanno

Ah! I missed that!

2025-09-25 22-50-38
Hanno

@Lavin, try the SetHeaderValue action in the attached .oap in the OnResponse callback of your Exposed REST API and let me know if it solves your problem.

RESTExtender.oap
UserImage.jpg
Lavin

Hanno wrote:

@Lavin, try the SetHeaderValue action in the attached .oap in the OnResponse callback of your Exposed REST API and let me know if it solves your problem.

@Hanno Any chance I can use it in Outsystems 10? 


2025-09-25 22-50-38
Hanno

I do not have access to a P10 server, unfortunately. You might need to code it yourself. 

Are you comfortable with building/modifying an extension?

2025-09-25 22-50-38
Hanno

Lavin, try the attached?

REST.Extender1.0.0O10.xif
UserImage.jpg
Lavin

Hanno wrote:

Lavin, try the attached?

Tried it but got error message "This operation requires IIS integrated pipeline mode". I am trying to solve that now.


UserImage.jpg
Lavin

Hanno wrote:

Lavin, try the attached?

@Hanno Tried the extension using the setHeader action. But the response still return the new  Access-Control-Allow-Origin header that I set and the default  Access-Control-Allow-Origin header set as *.

2025-09-25 22-50-38
Hanno

Lavin, I used the AddHeader action from HTTPRequestHandler in an API I exposed. 

The result is as below. I only get one Access-Control-Allow-Origin header with the value I set.

UserImage.jpg
Lavin

Hanno wrote:

Lavin, I used the AddHeader action from HTTPRequestHandler in an API I exposed. 

The result is as below. I only get one Access-Control-Allow-Origin header with the value I set.


My request header have an origin: https://localhost. If add that the additional Access-Control-Allow-Origin: * will appear.


2025-09-25 22-50-38
Hanno
Solution

It sounds like it might be an IIS config.

Can you perhaps see if your IIS is adding/sending any CORS headers?

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.