HTTP Authorization Header in EventSource (Server Sent Events)

前端 未结 8 1994
梦谈多话
梦谈多话 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.... }});

提交回复
热议问题