API: Richieste HTTP
Funzioni per effettuare richieste HTTP a servizi e API esterne.
Registrazione C++: regWeb(js).
Implementazione: jsWeb.h + NetCurl.h (wrapper su libcurl).
http_get
http_get(url: string) → object
Esegue una richiesta HTTP GET.
Parametri:
| Parametro | Tipo | Descrizione |
|---|---|---|
url |
string | URL completo incluso il protocollo (https://...) |
Risultato:
| Campo | Tipo | Descrizione |
|---|---|---|
status |
number | Codice di risposta HTTP (200, 404, 500, ecc.) |
body |
string | Corpo della risposta |
error |
string | Messaggio di errore curl o "" |
Esempio:
let r = http_get("https://api.example.com/health");
if (r.error) {
console.log("Errore richiesta:", r.error);
} else if (r.status !== 200) {
console.log("Errore HTTP:", r.status);
} else {
let data = JSON.parse(r.body);
console.log("Risposta:", JSON.stringify(data));
}
http_post
http_post(url: string, body: string, headers?: string[]) → object
Esegue una richiesta HTTP POST.
Parametri:
| Parametro | Tipo | Descrizione |
|---|---|---|
url |
string | URL completo |
body |
string | Corpo della richiesta (JSON, dati form, ecc.) |
headers |
string[] | (opzionale) Array di intestazioni HTTP |
Formato intestazioni:
["Content-Type: application/json", "Authorization: Bearer TOKEN"]
Risultato:
| Campo | Tipo | Descrizione |
|---|---|---|
status |
number | Codice di risposta HTTP |
body |
string | Corpo della risposta |
error |
string | Messaggio di errore o "" |
Esempi:
// Invia JSON
let payload = JSON.stringify({ event: "alert", level: "critical" });
let r = http_post(
"https://api.example.com/events",
payload,
["Content-Type: application/json"]
);
console.log(r.status, r.body);
// Con autorizzazione
let r2 = http_post(
"https://api.example.com/data",
JSON.stringify({ value: 42 }),
[
"Content-Type: application/json",
"Authorization: Bearer my_token_here"
]
);
// Senza intestazioni (es. dati form)
let r3 = http_post(
"https://example.com/form",
"name=John&age=30"
);
Lavorare con i webhook
Discord
let url = "https://discord.com/api/webhooks/ID/TOKEN";
let r = http_post(url, JSON.stringify({
content: "Allerta! Servizio non funzionante."
}), ["Content-Type: application/json"]);
console.log(r.status); // 204 = successo
Slack
let url = "https://hooks.slack.com/services/T.../B.../...";
let r = http_post(url, JSON.stringify({
text: "Allerta! Servizio non funzionante."
}), ["Content-Type: application/json"]);
console.log(r.status); // 200 = successo
Telegram
let token = "BOT_TOKEN";
let chatId = "CHAT_ID";
let text = encodeURIComponent("Allerta! Servizio non funzionante.");
let r = http_get(
"https://api.telegram.org/bot" + token +
"/sendMessage?chat_id=" + chatId + "&text=" + text
);
let resp = JSON.parse(r.body);
console.log(resp.ok); // true = successo
Gestione degli errori
function safePost(url, payload, headers) {
let r = http_post(url, payload, headers);
// Errore di rete (curl)
if (r.error) {
console.log("[ERRORE] Errore di rete:", r.error);
return false;
}
// Errore HTTP
if (r.status < 200 || r.status >= 300) {
console.log("[ERRORE] HTTP", r.status, ":", r.body);
return false;
}
return true;
}
Codici di risposta HTTP
| Codice | Significato |
|---|---|
| 200 | OK |
| 201 | Creato |
| 204 | Nessun Contenuto (successo webhook Discord) |
| 400 | Richiesta Non Valida |
| 401 | Non Autorizzato |
| 403 | Vietato |
| 404 | Non Trovato |
| 429 | Troppe Richieste (limite di frequenza) |
| 500 | Errore Interno del Server |