Add non-json support for http body

This commit is contained in:
Justin Tisdale 2022-08-11 20:57:03 -04:00
parent 3a18801722
commit 2b9bf095a6
5 changed files with 34 additions and 7 deletions

View file

@ -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;

View file

@ -62,6 +62,7 @@ class Database {
"patch-add-clickable-status-page-link.sql": true, "patch-add-clickable-status-page-link.sql": true,
"patch-add-sqlserver-monitor.sql": true, "patch-add-sqlserver-monitor.sql": true,
"patch-add-other-auth.sql": { parents: [ "patch-monitor-basic-auth.sql" ] }, "patch-add-other-auth.sql": { parents: [ "patch-monitor-basic-auth.sql" ] },
"patch-http-body-encoding.sql": true
}; };
/** /**

View file

@ -103,6 +103,7 @@ class Monitor extends BeanModel {
authMethod: this.authMethod, authMethod: this.authMethod,
authWorkstation: this.authWorkstation, authWorkstation: this.authWorkstation,
authDomain: this.authDomain, authDomain: this.authDomain,
httpBodyEncoding: this.httpBodyEncoding
}; };
if (includeSensitiveData) { if (includeSensitiveData) {
@ -241,16 +242,33 @@ class Monitor extends BeanModel {
log.debug("monitor", `[${this.name}] Prepare Options for axios`); 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 = { const options = {
url: this.url, url: this.url,
method: (this.method || "get").toLowerCase(), method: (this.method || "get").toLowerCase(),
...(this.body ? { data: JSON.parse(this.body) } : {}), ...(bodyValue ? { data: bodyValue } : {}),
timeout: this.interval * 1000 * 0.8, timeout: this.interval * 1000 * 0.8,
headers: { 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", "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, "User-Agent": "Uptime-Kuma/" + version,
...(this.headers ? JSON.parse(this.headers) : {}), ...(this.headers ? JSON.parse(this.headers) : {}),
...(basicAuthHeader), ...(basicAuthHeader),
...(contentType ? { "Content-Type": contentType } : {})
}, },
maxRedirects: this.maxredirects, maxRedirects: this.maxredirects,
validateStatus: (status) => { validateStatus: (status) => {

View file

@ -693,6 +693,7 @@ let needSetup = false;
bean.authMethod = monitor.authMethod; bean.authMethod = monitor.authMethod;
bean.authWorkstation = monitor.authWorkstation; bean.authWorkstation = monitor.authWorkstation;
bean.authDomain = monitor.authDomain; bean.authDomain = monitor.authDomain;
bean.httpBodyEncoding = monitor.httpBodyEncoding;
await R.store(bean); await R.store(bean);

View file

@ -398,8 +398,8 @@
<!-- Encoding --> <!-- Encoding -->
<div class="my-3"> <div class="my-3">
<label for="bodyEncoding" class="form-label">{{ $t("Body Encoding") }}</label> <label for="httpBodyEncoding" class="form-label">{{ $t("Body Encoding") }}</label>
<select id="bodyEncoding" v-model="monitor.bodyEncoding" class="form-select"> <select id="httpBodyEncoding" v-model="monitor.httpBodyEncoding" class="form-select">
<option value="json"> <option value="json">
JSON JSON
</option> </option>
@ -660,7 +660,7 @@ export default {
mqttTopic: "", mqttTopic: "",
mqttSuccessMessage: "", mqttSuccessMessage: "",
authMethod: null, authMethod: null,
bodyEncoding: null httpBodyEncoding: "json"
}; };
if (this.$root.proxyList && !this.monitor.proxyId) { if (this.$root.proxyList && !this.monitor.proxyId) {
@ -698,7 +698,8 @@ export default {
* @returns {boolean} Is the form input valid? * @returns {boolean} Is the form input valid?
*/ */
isInputValid() { isInputValid() {
if (this.monitor.body) { //TODO: Handle validation for all 3 possible options + null value
if (this.monitor.body && (!this.monitor.httpBodyEncoding || this.monitor.httpBodyEncoding === "json")) {
try { try {
JSON.parse(this.monitor.body); JSON.parse(this.monitor.body);
} catch (err) { } catch (err) {
@ -729,8 +730,8 @@ export default {
return; return;
} }
// Beautify the JSON format // Beautify the JSON format (only if httpBodyEncoding is not set or === json)
if (this.monitor.body) { if (this.monitor.body && (!this.monitor.httpBodyEncoding || this.monitor.httpBodyEncoding === "json")) {
this.monitor.body = JSON.stringify(JSON.parse(this.monitor.body), null, 4); this.monitor.body = JSON.stringify(JSON.parse(this.monitor.body), null, 4);
} }