Home > database >  How can I store the initial text/event-stream response from a get request in JavaScript when the ser
How can I store the initial text/event-stream response from a get request in JavaScript when the ser

Time:10-24

I'm trying to retrieve some simple values from an API. This is the request URL:

https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4

It seems that somehow the server puts the page in a constant refresh state. Whether in the browser or via my code, loading never completes, but I can see the JSON response in the browser. After about a minute, another JSON response is appended to the original in the browser.

This is the code snippet I'm currently using, and it doesn't seem to ever terminate although I've only waited a max of amount 5 minutes.

var response = UrlFetchApp.fetch("https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4")
var json = JSON.parse(response.getContentText())

Is there a way for me to terminate the never-ending loop and retrieve what I already have as a response?

Edit:

As @JeremyThille pointed out, the response type is text/event-stream but am still unsure of who to handle this type of response.

CodePudding user response:

Looks like the proper way to handle text/event-stream responses is usually with EventSource however, I am unable to use external libraries for my use case in Google Sheets.

var source = new EventSource('https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4');
source.addEventListener('message', function(e) {
  console.log(e.data);
}, false);
  • Related