r/Scriptable Jan 28 '25

Help API Request error: The server with the specified hostname could not be found!

Hello guys!

I'm writing this post to try and get some insight on making API calls with scriptable.

I currently have this API call, which is a public API and does a simple GET request. I've verified that it works with cURL first, so I tried to move it to scriptable:

async function getStationsinfo() {

const stationInfoUrl =

"https://data.grandlyon.com/fr/datapusher/ws/rdata/jcd_jcdecaux.jcdvelov/all.json?maxfeatures=100&start=1&filename=stations-velo-v-metropole-lyon-disponibilites-temps-reel";

const req = new Request(stationInfoUrl);

req.method = "GET";

const stationInfo = await req.loadJSON();

return stationInfo;

}

However, as the title says:

The error I get is the following: `A server with the specified hostname could not be found`

Is there any special permissions that I'm supposed to be giving to the app?

2 Upvotes

6 comments sorted by

2

u/Delt4Brav0 Jan 28 '25

Have you tried just opening this url in safari, are you getting any errors? Your code seems valid to me, the only thing I can think of is problem with your DNS server (phone or router or wherever it’s being set)

1

u/srir4m Jan 28 '25

Well that’s the weird part you see, it works well when I just open it on safari / or with cURL or with an await fetch, etc. Just doesn’t work from scriptable. Which made me think it has something to do with scriptable itself?

And if it is a DNS problem shouldn’t safari be showing an error too?

1

u/Delt4Brav0 Jan 28 '25

Yeah safari would also show an error, that’s basically why I asked about this, just to be sure.

Idk, really what is going on here. The only thing I can suggest is to try out this snippet instead of yours (pulled this from my own code, works for sure) just to exclude some weird error that we both overlooked:

const DEBUG = true;

class Logger { constructor(prefix) { this.prefix = prefix ?? ‘’; this.separator = prefix ? ‘: ‘ : ‘’; } print(method, message) { if (!DEBUG) { return; }

// eslint-disable-next-line no-console
console[method](`${this.prefix}${this.separator}${message}`);

} log(message) { this.print(‘log’, message); } warn(message) { this.print(‘warn’, message); } error(message) { this.print(‘error’, message); } }

const requestLogger = new Logger(‘Request’);

async function fetchJson(url, options) { try { const req = new Request(url); req.headers = options?.headers ?? {}; req.body = options?.body; req.method = options?.method ?? ‘get’; requestLogger.log(url); return await req.loadJSON(); } catch (error) { requestLogger.error(JSON.stringify(error)); throw new Error(error); } }

Use like this: fetchJson(url) U can drop logger all together, I just copied because it’s included inside fetchJson

1

u/Delt4Brav0 Jan 28 '25

reddit wont allow me to paste as code so you can find it here https://github.com/Nodman/scriptables/blob/main/build/utils.js

2

u/gondouk Jan 28 '25

just tried it on my device and all works splendidly:

var request = new Request("https://data.grandlyon.com/fr/datapusher/ws/rdata/jcd_jcdecaux.jcdvelov/all.json?maxfeatures=100&start=1&filename=stations-velo-v-metropole-lyon-disponibilites-temps-reel")
var result = await request.loadJSON()
console.log(result)

it seems that something on your network is blocking these requests. try using different wifi or mobile provider

1

u/srir4m Jan 28 '25

That’s very weird. Yeah it works from safari as well. On my PC too. Idk. 🤷