HTTP Authorization Header in EventSource (Server Sent Events)

前端 未结 8 1992
梦谈多话
梦谈多话 2021-02-01 01:52

I need to set an Authorization header to an HTML5 EventSource. As Server Sent Events seems to be disused since Websockets appeared, I cannot find any useful documentation. The a

相关标签:
8条回答
  • 2021-02-01 02:28

    If you use this fork of the event-source polyfill you will be able to add authorization headers similarly to the way rafaelzlisboa describes: https://github.com/AlexGalays/EventSource#923b9a0998fcfd7753040e09aa83764b3cc0230d

    Ï don't know if you can provide the authentication header as a second argument like in rafaelzlisboa's example, I got it to work by creating a headers object, and putting my authorization header in there, like this:

    new EventSource("https://domain/stream", { headers: { Authorization: Bearer.... }});

    0 讨论(0)
  • 2021-02-01 02:31

    EventSource doesn't have an API for sending HTTP headers to server. I was struggling with this problem too when I was building realtime-chat using SSE.

    However I think cookies will be sent automatically if your SSE server is the same server as your authentication server.

    0 讨论(0)
提交回复
热议问题