Book a Demo

Author Topic: How to use Fetch() for JSON REST API calls?  (Read 15532 times)

ea0522

  • EA User
  • **
  • Posts: 134
  • Karma: +5/-0
    • View Profile
How to use Fetch() for JSON REST API calls?
« on: May 30, 2024, 09:13:15 pm »
Hai, I'm trying to import data in Enterprise Architect using a REST API call.
The REST API returns a nicely formatted JSON string.
In another javascript, I've managed to parse a valid JSON string.
However, when I try to use Fetch() to get the information from an URL, I get an error: "fetch is not defined".
Anyone has an example javascript I can adapt to test the fetch function?
Thanks.

jvduuren

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: How to use Fetch() for JSON REST API calls?
« Reply #1 on: September 18, 2025, 08:18:51 pm »
You can simulate the fetch command with the following JavaScript example:

Code: [Select]
const http2 = new COMObject("MSXML2.ServerXMLHTTP");
http2.setTimeouts(15000, 15000, 15000, 900000);
http2.setProxy(2,"http://localhost:9000","");
http2.open("POST", url, false); // Use "true" for async
http2.setRequestHeader("Content-Type", "application/json");
http2.setRequestHeader("Authorization", "Bearer " + OPENAI_API_KEY);
http2.send(JSON.stringify(body));

if (http2.status == 200) {
Session.Output(http2.responseText);
} else {
 Session.Output("Error: " + http2.status + " - " + http2.statusText);
}