reactjs - React Server Sent Events Unauthorized 401 - Stack Overflow

admin2025-04-17  3

I am using server-sent events in my React project. While it is working on Postman, I am receiving 401 unauthorized on frontend implementation.

useEffect(() => {
const token = localStorage.getItem("authToken");
const evtSource = new EventSource(
  `http://localhost:5001/scienceacademyapi/v1/notification/stream?token=${token}`
);
evtSource.onmessage = (event) => {
  const newNotification = JSON.parse(event.data);
  setShowNotifications((prev) => [...prev, newNotification]);
};

evtSource.onerror = (err) => {
  console.error("Error with sse : ", err);
  evtSource.close();
};

return () => {
  evtSource.close();
};
}, []);
转载请注明原文地址:http://anycun.com/QandA/1744852611a88529.html