bark-worker icon indicating copy to clipboard operation
bark-worker copied to clipboard

body 中带有%会导致500错误

Open cbmland opened this issue 1 month ago • 1 comments

curl -X "POST" "https://bark.xxx.com/push"
-H 'Content-Type: application/json; charset=utf-8'
-d $'{ "title": "我的法克", "body": "提醒充电Bark 【电量预警】已达到电量预警下限,请及时充电! 剩余电量:76% 充满电量:100 当前电压:4.14V 当前温度:32.30℃ 电池状态:放电中 健康度:良好 充电器:未知 提醒充电Bark 2025-11-17 18:29:47 ", "group": "测试", "device_keys": ["111111","22222"] }' {"code":200,"message":"success","data":[{"code":500,"device_key":"111111"},{"code":500,"device_key":"22222"}],"timestamp":1763376859}%

cbmland avatar Nov 17 '25 11:11 cbmland

发现问题代码 try { if (title) { title = decodeURIComponent(title.replaceAll("\+","%20")) } if (subtitle) { subtitle = decodeURIComponent(subtitle.replaceAll("\+","%20")) } if (body) { console.log('body1',body) body = decodeURIComponent(body.replaceAll("\+","%20")) console.log('body2',body) } }

chatGPT的解决方案:

✅ 最稳妥修复:先将非法“裸百分号”转义成 %25

给你一个专门用于安全解码的函数,完全解决问题: function safeDecode(str) { if (!str) return str;

// 把所有不构成 %xx 的裸 % 转为 %25 // 匹配规则:% 后面不是 2 位十六进制字符 str = str.replace(/%(?![0-9A-Fa-f]{2})/g, "%25");

// 再把 + 转成空格(URL 中 + 替代空格) str = str.replace(/+/g, "%20");

try { return decodeURIComponent(str); } catch (e) { // 最终兜底:失败就返回原始字符串 return str; } }

然后你的代码改成: try { if (title) title = safeDecode(title); if (subtitle) subtitle = safeDecode(subtitle);

if (body) {
    console.log('body1', body)
    body = safeDecode(body)
    console.log('body2', body)
}

}

cbmland avatar Nov 17 '25 11:11 cbmland

Fixed in commit dc2a328ea4f048e1e93bf14ffe6840b8ff1db217

cwxiaos avatar Dec 03 '25 02:12 cwxiaos