diff --git a/db/patch-http-body-encoding.sql b/db/patch-http-body-encoding.sql new file mode 100644 index 000000000..322c8b893 --- /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 449f16d55..575827f34 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 d6e1896a0..b071a6221 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 18598171e..57a6a8fa7 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 478ddddca..83bc22316 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 297759c57..d2a418a55 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -503,6 +503,15 @@ + +