如何消除R代码中字符串多余字符以实现Teams通知正常发送
R代码发送Teams通知的字符串格式问题
问题描述
我写了一段R代码,用于遍历表格内容并向Teams发送通知,但代码因字符串格式错误抛出异常。
原代码如下:
# Loop through each message and send Teams notification for (i in seq_along(messages)) { message <- messages[[i]] payload <- list(text = message) json_payload <- gsub("\"", "\\\"", jsonlite::toJSON(payload)) # escape double quotes with a single backslash command <- sprintf('curl -H "Content-Type: application/json" -d "%s" %s', json_payload, teams_webhook_url) # use single quotes for the command string and double quotes for the header value system(command, intern = TRUE) # use intern = TRUE to capture the output of the system command print(command) }
打印出的command内容不符合要求:
[1] "curl -H "Content-Type: application/json" -d "{"text":["Subnet 10.0.0.0 has a higher than usual round trip time: 0.07 ms"]}" https://my.webhook.office.com/webhookb2/webhook"
正确的格式应该是:
curl -H "Content-Type: application/json" -d "{\"text\": \"Subnet 100.95.13.0 has a higher than usual round trip time: 0.25 ms\"}" https://my.webhook.office.com/webhookb2/webhook"
需要消除的多余字符:
- 开头的引号
Content-Type:前的反斜杠json后的反斜杠{"text":前的反斜杠- 包裹文本的方括号
[] ms"}后的反斜杠
尝试过的无效方案
我试了多种调整方式,但都没能修正字符串格式:
json_payload <- gsub("\"", "\\\\\\\"", jsonlite::toJSON(payload)) for (i in seq_along(messages)) { message <- messages[[i]] payload <- list(text = message) json_payload <- gsub("\"", "\\\\\"", jsonlite::toJSON(payload)) # escape double quotes with three backslashes command <- sprintf("curl -H 'Content-Type: application/json' -d '%s' %s", json_payload, teams_webhook_url) output <- system(command, intern = TRUE) output <- gsub("^\"", "", output) # remove the first " cat(output, "\n") } for (i in seq_along(messages)) { message <- messages[[i]] payload <- list(text = message) json_payload <- jsonlite::toJSON(payload, auto_unbox = TRUE, escape_unicode = FALSE) command <- sprintf('curl -H "Content-Type: application/json" -d \'%s\' %s', json_payload, teams_webhook_url) system(command, intern = TRUE) } for (i in seq_along(messages)) { message <- messages[[i]] payload <- list(text = message) json_payload <- gsub('"', '\\\\\"', jsonlite::toJSON(payload)) command <- sprintf("curl -H \"Content-Type: application/json\" -d '%s' %s", json_payload, teams_webhook_url) command <- gsub("\\[|\\]", "", command) command <- gsub("\\\\", "", command) system(command, intern = TRUE) }
可行解决方案
方案1:修复curl命令的转义逻辑
问题根源在于jsonlite::toJSON默认会把单个字符串包装成数组(产生方括号),同时shell环境下的双引号转义逻辑错误。调整代码如下:
for (i in seq_along(messages)) { message <- messages[[i]] # 用auto_unbox=TRUE取消数组包装,直接输出标量值 payload <- list(text = message) json_payload <- jsonlite::toJSON(payload, auto_unbox = TRUE) # 对JSON中的双引号做shell适配的转义 escaped_json <- gsub('"', '\\"', json_payload) # 构建正确的curl命令,外层用单引号避免嵌套冲突 command <- sprintf('curl -H "Content-Type: application/json" -d "%s" "%s"', escaped_json, teams_webhook_url) # 执行命令 system(command, intern = TRUE) print(command) }
方案2:使用httr包直接发送请求(更推荐)
避免调用系统curl带来的转义问题,直接用R的httr包发送POST请求,自动处理JSON格式和请求头:
library(httr) for (i in seq_along(messages)) { message <- messages[[i]] payload <- list(text = message) # 发送POST请求,自动编码JSON并设置请求头 response <- POST( url = teams_webhook_url, body = payload, encode = "json", add_headers("Content-Type" = "application/json") ) # 可选:打印响应状态码,确认请求是否成功 print(status_code(response)) }
内容的提问来源于stack exchange,提问作者Alienvolm
相关产品推荐
相关产品推荐

