diff --git a/db/patch-http-body-encoding.sql b/db/patch-http-body-encoding.sql new file mode 100644 index 000000000..de02bede1 --- /dev/null +++ b/db/patch-http-body-encoding.sql @@ -0,0 +1,6 @@ +-- 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 TEXT; + +COMMIT; diff --git a/server/database.js b/server/database.js index b234d67d0..ecf6af728 100644 --- a/server/database.js +++ b/server/database.js @@ -62,6 +62,7 @@ class Database { "patch-add-clickable-status-page-link.sql": true, "patch-add-sqlserver-monitor.sql": true, "patch-add-other-auth.sql": { parents: [ "patch-monitor-basic-auth.sql" ] }, + "patch-http-body-encoding.sql": true }; /** diff --git a/server/model/monitor.js b/server/model/monitor.js index 2feef1356..af3d162a5 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -103,6 +103,7 @@ class Monitor extends BeanModel { authMethod: this.authMethod, authWorkstation: this.authWorkstation, authDomain: this.authDomain, + httpBodyEncoding: this.httpBodyEncoding }; if (includeSensitiveData) { @@ -241,16 +242,33 @@ class Monitor extends BeanModel { log.debug("monitor", `[${this.name}] Prepare Options for axios`); + // Set content-type header and body values based on the httpBodyEncoding type selected + // TODO: Check if this.headers already contains a content-type header set by the user; if so, don't inject one + let bodyValue = null; + let contentType = 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"; + } else if (this.body && (this.httpBodyEncoding === "form")) { + bodyValue = this.body; + contentType = "application/x-www-form-urlencoded"; + } + 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) : {}), ...(basicAuthHeader), + ...(contentType ? { "Content-Type": contentType } : {}) }, maxRedirects: this.maxredirects, validateStatus: (status) => { diff --git a/server/server.js b/server/server.js index 2a2c4bf61..616a10cda 100644 --- a/server/server.js +++ b/server/server.js @@ -693,6 +693,7 @@ let needSetup = false; bean.authMethod = monitor.authMethod; bean.authWorkstation = monitor.authWorkstation; bean.authDomain = monitor.authDomain; + bean.httpBodyEncoding = monitor.httpBodyEncoding; await R.store(bean); diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 7dcc7d64b..b4aceba7b 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -398,8 +398,8 @@