diff --git a/db/patch-http-body-encoding.sql b/db/patch-http-body-encoding.sql new file mode 100644 index 00000000..322c8b89 --- /dev/null +++ b/db/patch-http-body-encoding.sql @@ -0,0 +1,12 @@ +-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. +BEGIN TRANSACTION; + +ALTER TABLE monitor ADD http_body_encoding VARCHAR(25); + +COMMIT; + +BEGIN TRANSACTION; + +UPDATE monitor SET http_body_encoding = 'json' WHERE (type = 'http' or type = 'keyword') AND http_body_encoding IS NULL; + +COMMIT; diff --git a/server/database.js b/server/database.js index 449f16d5..575827f3 100644 --- a/server/database.js +++ b/server/database.js @@ -70,6 +70,7 @@ class Database { "patch-maintenance-table2.sql": true, "patch-add-gamedig-monitor.sql": true, "patch-add-google-analytics-status-page-tag.sql": true, + "patch-http-body-encoding.sql": true }; /** diff --git a/server/model/monitor.js b/server/model/monitor.js index d6e1896a..b071a622 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -111,6 +111,7 @@ class Monitor extends BeanModel { radiusCalledStationId: this.radiusCalledStationId, radiusCallingStationId: this.radiusCallingStationId, game: this.game, + httpBodyEncoding: this.httpBodyEncoding }; if (includeSensitiveData) { @@ -272,17 +273,29 @@ class Monitor extends BeanModel { log.debug("monitor", `[${this.name}] Prepare Options for axios`); + let contentType = null; + let bodyValue = null; + + if (this.body && !this.httpBodyEncoding || this.httpBodyEncoding === "json") { + bodyValue = JSON.parse(this.body); + contentType = "application/json"; + } else if (this.body && (this.httpBodyEncoding === "xml")) { + bodyValue = this.body; + contentType = "text/xml; charset=utf-8"; + } + // Axios Options const options = { url: this.url, method: (this.method || "get").toLowerCase(), - ...(this.body ? { data: JSON.parse(this.body) } : {}), + ...(bodyValue ? { data: bodyValue } : {}), timeout: this.interval * 1000 * 0.8, headers: { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "User-Agent": "Uptime-Kuma/" + version, - ...(this.headers ? JSON.parse(this.headers) : {}), + ...(contentType ? { "Content-Type": contentType } : {}), ...(basicAuthHeader), + ...(this.headers ? JSON.parse(this.headers) : {}) }, maxRedirects: this.maxredirects, validateStatus: (status) => { diff --git a/server/server.js b/server/server.js index 18598171..57a6a8fa 100644 --- a/server/server.js +++ b/server/server.js @@ -726,6 +726,7 @@ let needSetup = false; bean.radiusCalledStationId = monitor.radiusCalledStationId; bean.radiusCallingStationId = monitor.radiusCallingStationId; bean.radiusSecret = monitor.radiusSecret; + bean.httpBodyEncoding = monitor.httpBodyEncoding; bean.validate(); diff --git a/src/lang/en.json b/src/lang/en.json index 478ddddc..83bc2231 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -671,5 +671,6 @@ "Google Analytics ID": "Google Analytics ID", "Edit Tag": "Edit Tag", "Server Address": "Server Address", - "Learn More": "Learn More" + "Learn More": "Learn More", + "Body Encoding": "Body Encoding" } diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 297759c5..d2a418a5 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -503,6 +503,15 @@ + +
+ + +
+
@@ -723,6 +732,15 @@ message HealthCheckResponse { ` ]); }, bodyPlaceholder() { + if (this.monitor && this.monitor.httpBodyEncoding && this.monitor.httpBodyEncoding === "xml") { + return this.$t("Example:", [ ` + + + + Kuma + +` ]); + } return this.$t("Example:", [ ` { "key": "value" @@ -872,6 +890,7 @@ message HealthCheckResponse { mqttTopic: "", mqttSuccessMessage: "", authMethod: null, + httpBodyEncoding: "json" }; if (this.$root.proxyList && !this.monitor.proxyId) { @@ -909,7 +928,7 @@ message HealthCheckResponse { * @returns {boolean} Is the form input valid? */ isInputValid() { - if (this.monitor.body) { + if (this.monitor.body && (!this.monitor.httpBodyEncoding || this.monitor.httpBodyEncoding === "json")) { try { JSON.parse(this.monitor.body); } catch (err) { @@ -933,6 +952,7 @@ message HealthCheckResponse { * @returns {void} */ async submit() { + this.processing = true; if (!this.isInputValid()) { @@ -940,11 +960,15 @@ message HealthCheckResponse { return; } - // Beautify the JSON format - if (this.monitor.body) { + // Beautify the JSON format (only if httpBodyEncoding is not set or === json) + if (this.monitor.body && (!this.monitor.httpBodyEncoding || this.monitor.httpBodyEncoding === "json")) { this.monitor.body = JSON.stringify(JSON.parse(this.monitor.body), null, 4); } + if (this.monitor.type && this.monitor.type !== "http" && this.monitor.type !== "keyword") { + this.monitor.httpBodyEncoding = null; + } + if (this.monitor.headers) { this.monitor.headers = JSON.stringify(JSON.parse(this.monitor.headers), null, 4); }