JuptyterLab Extension CORS Request

I am trying to fetch data from cross origin and getting the error.
Uncaught Error: Can only be used for notebook server requests

I am newbie and could not find any solution.

Sample code:

    const settings = ServerConnection.makeSettings();
    //settings.baseUrl = "https://api.ipify.org";
    console.log(settings)
    // uses default settings
    const url = 'https://api.ipify.org?format=json';  
    const init = {"method":"GET",  headers: {"Content-Type": "application/json",mode: "cors"}}
      // 'Content-Type': 'application/x-www-form-urlencoded'};  // fetch settings (see fetch API link above)
    ServerConnection.makeRequest(url, init, settings).then(response => { console.log('got', response); });

Do not use ServerConnection.makeRequest for requests to arbitrary domains (such as https://api.ipify.org in your case) - it is only meant for interacting with jupyter-server and its extensions. For generic queries you can use the generic Fetch API.

1 Like

Thank you for the answer, I am now using directly fetch.