API: HTTP Requests
Functions for making HTTP requests to external services and APIs.
C++ registration: regWeb(js).
Implementation: jsWeb.h + NetCurl.h (wrapper over libcurl).
http_get
http_get(url: string) → object
Perform an HTTP GET request.
Parameters:
| Parameter | Type | Description |
|---|---|---|
url |
string | Full URL including protocol (https://...) |
Result:
| Field | Type | Description |
|---|---|---|
status |
number | HTTP response code (200, 404, 500, etc.) |
body |
string | Response body |
error |
string | curl error message or "" |
Example:
let r = http_get("https://api.example.com/health");
if (r.error) {
console.log("Request error:", r.error);
} else if (r.status !== 200) {
console.log("HTTP error:", r.status);
} else {
let data = JSON.parse(r.body);
console.log("Response:", JSON.stringify(data));
}
http_post
http_post(url: string, body: string, headers?: string[]) → object
Perform an HTTP POST request.
Parameters:
| Parameter | Type | Description |
|---|---|---|
url |
string | Full URL |
body |
string | Request body (JSON, form data, etc.) |
headers |
string[] | (optional) Array of HTTP headers |
Header format:
["Content-Type: application/json", "Authorization: Bearer TOKEN"]
Result:
| Field | Type | Description |
|---|---|---|
status |
number | HTTP response code |
body |
string | Response body |
error |
string | Error message or "" |
Examples:
// Send 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);
// With authorization
let r2 = http_post(
"https://api.example.com/data",
JSON.stringify({ value: 42 }),
[
"Content-Type: application/json",
"Authorization: Bearer my_token_here"
]
);
// Without headers (e.g. form data)
let r3 = http_post(
"https://example.com/form",
"name=John&age=30"
);
Working with webhooks
Discord
let url = "https://discord.com/api/webhooks/ID/TOKEN";
let r = http_post(url, JSON.stringify({
content: "Alert! Service is down."
}), ["Content-Type: application/json"]);
console.log(r.status); // 204 = success
Slack
let url = "https://hooks.slack.com/services/T.../B.../...";
let r = http_post(url, JSON.stringify({
text: "Alert! Service is down."
}), ["Content-Type: application/json"]);
console.log(r.status); // 200 = success
Telegram
let token = "BOT_TOKEN";
let chatId = "CHAT_ID";
let text = encodeURIComponent("Alert! Service is down.");
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 = success
Error handling
function safePost(url, payload, headers) {
let r = http_post(url, payload, headers);
// Network error (curl)
if (r.error) {
console.log("[ERROR] Network error:", r.error);
return false;
}
// HTTP error
if (r.status < 200 || r.status >= 300) {
console.log("[ERROR] HTTP", r.status, ":", r.body);
return false;
}
return true;
}
HTTP response codes
| Code | Meaning |
|---|---|
| 200 | OK |
| 201 | Created |
| 204 | No Content (Discord webhook success) |
| 400 | Bad Request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not Found |
| 429 | Too Many Requests (rate limit) |
| 500 | Internal Server Error |