nodejs 向钉钉的 webhook 地址发送 post 请求,一直返回{"errcode":-1,"errmsg":"系统繁忙"}。使用 curl 的方式就没问题。

2017-04-13 15:39:08 +08:00
 sherry00
大致代码:
var https = require("https")
var url = require('url')
var querystring = require('querystring')

var postUrl="https://oapi.dingtalk.com/robot/send?access_token=xxxxx"
var post_option = url.parse(postUrl);
post_option.method = 'POST';

var contentstr = "测试"
//var post_data = querystring.stringify({ //解析的时候会丢失对象
var post_data = JSON.stringify({
"msgtype": "text",
"text": {
"content": contentstr
},
"at": {
"isAtAll": true
}
})

post_option.headers = {
'Content-Type': 'application/json',
'Content-Length': post_data.length
}

var post_req = https.request(post_option, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk)
})
})

post_req.write(post_data);
post_req.end();

------------
返回结果是{"errcode":-1,"errmsg":"系统繁忙"}
官方文档都没的查,郁闷呢。

下面这种方式就没问题
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx' \
-H 'Content-Type: application/json' \
-d '
{"msgtype": "text",
"text": {
"content": " 我就是我, 是不一样的烟火"
}
}'

请大神们开智!感谢!
5396 次点击
所在节点    问与答
4 条回复
sherry00
2017-04-13 16:02:00 +08:00
不知道有什么好的办法能查明这个问题的原因
sherry00
2017-04-13 16:20:19 +08:00
我来结贴了。
原来是中文编码的问题引起的。
hughwu
2018-02-24 11:35:34 +08:00
你好,我也遇到了这个编码问题,请问你怎么解决的?
sherry00
2018-07-13 17:34:04 +08:00
@hughwu 'Content-Length': Buffer.byteLength(post_data, 'utf-8') //post_data.length 有中文不能用此方法

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/354628

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX