From a7b49fcd98b1f86db1ded571d252dc93fba14bab Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sat, 25 Feb 2023 17:28:32 +0800 Subject: [PATCH] Fix json body after xml body added --- server/model/monitor.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index b071a6221..ef29ba3b8 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -276,19 +276,24 @@ class Monitor extends BeanModel { 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"; + if (this.body && (typeof this.body === "string" && this.body.trim().length > 0)) { + if (!this.httpBodyEncoding || this.httpBodyEncoding === "json") { + try { + bodyValue = JSON.parse(this.body); + contentType = "application/json"; + } catch (e) { + throw new Error("Your JSON body is invalid. " + e.message); + } + } else if (this.httpBodyEncoding === "xml") { + bodyValue = this.body; + contentType = "text/xml; charset=utf-8"; + } } // Axios Options const options = { url: this.url, method: (this.method || "get").toLowerCase(), - ...(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", @@ -303,6 +308,10 @@ class Monitor extends BeanModel { }, }; + if (bodyValue) { + options.data = bodyValue; + } + if (this.proxy_id) { const proxy = await R.load("proxy", this.proxy_id);