新增:webhook 发送通道通过Content-Type=text/plain、text/html、text/css、text/javascript、text/xml指定请求体为文本格式

This commit is contained in:
pppscn 2024-10-17 22:41:59 +08:00
parent 785a3a2364
commit 5c19c10121

View File

@ -93,10 +93,19 @@ class WebhookUtils {
//通过`Content-Type=applicaton/json`指定请求体为`json`格式 //通过`Content-Type=applicaton/json`指定请求体为`json`格式
var isJson = false var isJson = false
//通过`Content-Type=text/plain、text/html、text/css、text/javascript、text/xml`指定请求体为`文本`格式
var isText = false
var mediaType = "text/plain"
for ((key, value) in setting.headers.entries) { for ((key, value) in setting.headers.entries) {
if (key.equals("Content-Type", ignoreCase = true) && value.contains("application/json")) { if (key.equals("Content-Type", ignoreCase = true)) {
if (value.contains("application/json")) {
isJson = true isJson = true
break break
} else if (value.startsWith("text/")) {
isText = true
mediaType = value
break
}
} }
} }
@ -138,7 +147,7 @@ class WebhookUtils {
} }
Log.d(TAG, "method = GET, Url = $requestUrl") Log.d(TAG, "method = GET, Url = $requestUrl")
XHttp.get(requestUrl).keepJson(true) XHttp.get(requestUrl).keepJson(true)
} else if (webParams.isNotEmpty() && (isJson || webParams.startsWith("{"))) { } else if (webParams.isNotEmpty() && (isJson || isText || webParams.startsWith("{"))) {
webParams = msgInfo.replaceTemplate(webParams, "", "Gson") webParams = msgInfo.replaceTemplate(webParams, "", "Gson")
val bodyMsg = webParams.replace("[from]", from) val bodyMsg = webParams.replace("[from]", from)
.replace("[content]", escapeJson(content)) .replace("[content]", escapeJson(content))
@ -155,11 +164,19 @@ class WebhookUtils {
.replace("[timestamp]", timestamp.toString()) .replace("[timestamp]", timestamp.toString())
.replace("[sign]", sign) .replace("[sign]", sign)
Log.d(TAG, "method = ${setting.method}, Url = $requestUrl, bodyMsg = $bodyMsg") Log.d(TAG, "method = ${setting.method}, Url = $requestUrl, bodyMsg = $bodyMsg")
if (isText) {
when (setting.method) {
"PUT" -> XHttp.put(requestUrl).keepJson(true).upString(bodyMsg, mediaType)
"PATCH" -> XHttp.patch(requestUrl).keepJson(true).upString(bodyMsg, mediaType)
else -> XHttp.post(requestUrl).keepJson(true).upString(bodyMsg, mediaType)
}
} else {
when (setting.method) { when (setting.method) {
"PUT" -> XHttp.put(requestUrl).keepJson(true).upJson(bodyMsg) "PUT" -> XHttp.put(requestUrl).keepJson(true).upJson(bodyMsg)
"PATCH" -> XHttp.patch(requestUrl).keepJson(true).upJson(bodyMsg) "PATCH" -> XHttp.patch(requestUrl).keepJson(true).upJson(bodyMsg)
else -> XHttp.post(requestUrl).keepJson(true).upJson(bodyMsg) else -> XHttp.post(requestUrl).keepJson(true).upJson(bodyMsg)
} }
}
} else { } else {
if (webParams.isEmpty()) { if (webParams.isEmpty()) {
webParams = "from=[from]&content=[content]&timestamp=[timestamp]" webParams = "from=[from]&content=[content]&timestamp=[timestamp]"