Fetching html content from URL [Javascript in Script Labs (Office)]

676 views Asked by At

Simply put, I am trying to fetch the html content from url "http://anovamoeda.oinvestidordesucesso.com/IS/nmREPORT.asp?NM=2" from inside the Script Labs environment, when running through Excel. A simple fetch(url) returns this enigmatic error message:

TypeError {}
 description: "Failed to fetch"
 message: "Failed to fetch"
 number: -2147418113
▶__proto__: TypeError
▶constructor: function TypeError()
 message: ""
 name: "TypeError"
▶toString: function toString()
▶__proto__: Error

I could figure out that this is a network error (the promise returns rejected), which seems weird because I did also try to fetch JSON from an API and it worked fine (for instance, https://api.binance.com/sapi/v1/system/status for those willing to try). First I thought it might be due to my url having html content, not JSON, but it is not like it cannot parse the information, it doesn't even get information to try. I tried to add a parameter to the fetch call passing {headers: {'Content-Type': 'text/html'}}, but that didn't work. (Also, I'm a noob in JS.)

But I could fetch the information from inside Google Scripts, using their own UrlFetchApp API with a simple call to that fetch method.

Happy with any help as I could not find any.

2

There are 2 answers

2
ksh On

You don't show enough code so I am not sure what you are doing. That URL does not return json: it is an html document. if you want the text of the html document try this:

var response = await fetch("http://anovamoeda.oinvestidordesucesso.com/IS/nmREPORT.asp?NM=2");
if (response.ok) {
    var content = await response.text();
    // parse the content
    console.log(content);
}
else {
   // handle error response
}
0
Denny Ceccon On

I believe I figured out what is happening. According to this thread buried in the depths of GitHub, Script Lab supports only https. Sad world this we live in.