How to add header to response?

I am trying to consume an API at phoenix from angular, however, I got this error:

Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource

So, I thought I have to add this header as:

Plug.Conn.put_resp_header(conn, 'Access-Control-Allow-Origin', '*')

But I still get this error, any idea why?

1 Like

Do you do it this way:

conn = Plug.Conn.put_resp_header(conn, ‘Access-Control-Allow-Origin’, ‘*’)

i.e. re-assign the variable back to conn?

1 Like

I tried so, did not work, but I found this https://github.com/mschae/cors_plug I think it has the answer

4 Likes

yes it does extra stuff for you. Basically you’d need to handle OPTIONS request (in addition to GET one on the same path). If you use the plug_cors, it’ll do it for you

2 Likes

Cors plug is what you want for sure, well done :thumbsup:

2 Likes