Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set SSE request authorization header?

How can I send authorization header for OAuth2 in SSE in javascript? It seems like native JS EventSource does not support any headers to be sent when listening to SSE. Is there another library maybe that will allow me to do that? Does anyone have any idea how to do it?

like image 922
Gasim Avatar asked Mar 16 '17 09:03

Gasim


People also ask

How do I add authorization header in GET request?

To send a GET request with a Bearer Token authorization header, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.

How do I request a post with authorization header?

To send a request with the Bearer Token authorization header, you need to make an HTTP request and provide your Bearer Token with the "Authorization: Bearer {token}" header. A Bearer Token is a cryptic string typically generated by the server in response to a login request.

What API does SSE use?

SSE is designed to use the JavaScript EventSource API in order to subscribe to a stream of data in any popular browser. Through this interface a client requests a particular URL in order to receive an event stream. SSE is commonly used to send message updates or continuous data streams to a browser client.


1 Answers

The SSE standard does not support sending headers (or POST data). Only GET data and cookies. Your choices boil down to:

  • Do a login step first, get a session cookie, and then that cookie will be sent with your EventSource request.
  • Fall back on using XMLHttpRequest2, i.e. comet/longpoll techniques.
like image 196
Darren Cook Avatar answered Sep 26 '22 06:09

Darren Cook