ninenines/cowboy

Headers are not set when using cowboy_websocket

lightuponcloud opened this issue · 2 comments

I'm trying to set CORS headers for client to receive them before connection is upgraded to websockets:

init(Req0, _Opts) ->
    Req1 = cowboy_req:set_resp_header(<<"access-control-allow-methods">>, <<"GET, OPTIONS">>, Req0),
    Req2 = cowboy_req:set_resp_header(<<"access-control-allow-origin">>, <<"*">>, Req1),
   {cowboy_websocket, Req2, []}

But headers are not returned to client:

--- request header ---
GET /websocket HTTP/1.1
Upgrade: websocket
Host: devda-appno-1licji7tu8hhu-1135642714.us-east-2.elb.amazonaws.com
Origin: http://devda-appno-1licji7tu8hhu-1135642714.us-east-2.elb.amazonaws.com
Sec-WebSocket-Key: 80GYcVeDAS6pmeT4q/bAqA==
Sec-WebSocket-Version: 13
Connection: Upgrade
authorization: e56960e7-cf54-4bd1-85be-c5fc29f21ce6

How to set headers for cowboy_websocket ?

essen commented

You are showing me the request headers, not the response headers.

Sorry, nevermind. It works:

--- response header ---
HTTP/1.1 101 Switching Protocols
Date: Tue, 24 Jan 2023 13:37:32 GMT
Connection: upgrade
access-control-allow-methods: GET, OPTIONS
access-control-allow-origin: *
sec-websocket-accept: C1+kovLKFAR/j4efgYXAIxq4Yiw=
server: Cowboy
upgrade: websocket

Thank you for your prompt response. It is greatly appreciated.