Tom Van Oosterwyck · Dec 3, 2019

Retrieving headers from websocket upgrades


I have a couple of IOT devices that communicate via WebSockets to our backend.

I successfully managed to create a websocket connection between server and (iot)client using %CSP.WebSocket
However I would like to implement authentication on this using the HTTP headers at the same time of Connection: Upgrade.

I tried to set the headers to globals in order to debug but they are always empty.

Example code: 

Method OnPreServer() As %Status
Try {
    s ^Header("HTTP_AUTHORIZATION")=%request.GetCgiEnv("HTTP_AUTHORIZATION")
    s ^Header("AUTHORIZATION")=%request.GetCgiEnv("AUTHORIZATION")
    s ^Header("REMOTE_USE")=%request.GetCgiEnv("REMOTE_USER")
    s ^Header("Sec-WebSocket-Protocol")=%request.GetCgiEnv("Sec-WebSocket-Protocol")
  } Catch {
    g ^%ETN
  q $$$OK

Is there an other way to access the http headers before completing the websocket connection?

0 161
Discussion (1)1
Log in or sign up to continue

I think, the best way would be to have REST entrypoint, which would redirect also work with WebSockets, while REST api can handle any Authentication issues. As an example, you can look at %API.Atelier.v1 class and method RunDebugger, which redirects control WebSocket.