feat(http-requests): add support for methods, body and headers for http

This commit is contained in:
Bert Verhelst
2021-10-02 16:48:27 +02:00
parent c93f42794f
commit 3f0b85e5a8
8 changed files with 144 additions and 7 deletions

View File

@@ -49,6 +49,7 @@ class Database {
"patch-incident-table.sql": true,
"patch-group-table.sql": true,
"patch-monitor-push_token.sql": true,
"patch-http-monitor-method-body-and-headers.sql": true,
}
/**

View File

@@ -53,6 +53,9 @@ class Monitor extends BeanModel {
id: this.id,
name: this.name,
url: this.url,
method: this.method,
body: this.body,
headers: this.headers,
hostname: this.hostname,
port: this.port,
maxretries: this.maxretries,
@@ -95,6 +98,31 @@ class Monitor extends BeanModel {
return JSON.parse(this.accepted_statuscodes_json);
}
/**
* Convert header string into an object:
* eg:
*
* Authorization: Basic <credentials>
* Content-Type: application/json
*
* into
*
* {
* "Authorization": "Basic <credentials>",
* "Content-Type": "application/json"
* }
**/
getParsedHeaders() {
if (!this.headers || !this.headers.includes(":")) {
return {};
}
return Object.fromEntries(this.headers.split("\n").map(header => {
const trimmedHeader = header.trim();
const firstColonIndex = trimmedHeader.indexOf(":");
return [trimmedHeader.slice(0, firstColonIndex), trimmedHeader.slice(firstColonIndex + 1) || ""];
}).filter(arr => !!arr[0] && !!arr[1]));
}
start(io) {
let previousBeat = null;
let retries = 0;
@@ -136,11 +164,15 @@ class Monitor extends BeanModel {
// Do not do any queries/high loading things before the "bean.ping"
let startTime = dayjs().valueOf();
let res = await axios.get(this.url, {
const options = {
url: this.url,
method: (this.method || "get").toLowerCase(),
...(this.body ? { data: JSON.parse(this.body) } : {}),
timeout: this.interval * 1000 * 0.8,
headers: {
"Accept": "*/*",
"User-Agent": "Uptime-Kuma/" + version,
...this.getParsedHeaders(),
},
httpsAgent: new https.Agent({
maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940)
@@ -150,7 +182,8 @@ class Monitor extends BeanModel {
validateStatus: (status) => {
return checkStatusCode(status, this.getAcceptedStatuscodes());
},
});
};
let res = await axios.request(options);
bean.msg = `${res.status} - ${res.statusText}`;
bean.ping = dayjs().valueOf() - startTime;

View File

@@ -505,6 +505,9 @@ exports.entryPage = "dashboard";
bean.name = monitor.name;
bean.type = monitor.type;
bean.url = monitor.url;
bean.method = monitor.method;
bean.body = monitor.body;
bean.headers = monitor.headers;
bean.interval = monitor.interval;
bean.retryInterval = monitor.retryInterval;
bean.hostname = monitor.hostname;
@@ -1028,6 +1031,9 @@ exports.entryPage = "dashboard";
name: monitorListData[i].name,
type: monitorListData[i].type,
url: monitorListData[i].url,
method: monitorListData[i].method || "GET",
body: monitorListData[i].body,
headers: monitorListData[i].headers,
interval: monitorListData[i].interval,
retryInterval: retryInterval,
hostname: monitorListData[i].hostname,