V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
sherry00
V2EX  ›  问与答

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

  •  
  •   sherry00 · 2017-04-13 15:39:08 +08:00 · 5385 次点击
    这是一个创建于 2562 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大致代码:
    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": " 我就是我, 是不一样的烟火"
    }
    }'

    请大神们开智!感谢!
    4 条回复    2018-07-13 17:34:04 +08:00
    sherry00
        1
    sherry00  
    OP
       2017-04-13 16:02:00 +08:00
    不知道有什么好的办法能查明这个问题的原因
    sherry00
        2
    sherry00  
    OP
       2017-04-13 16:20:19 +08:00
    我来结贴了。
    原来是中文编码的问题引起的。
    hughwu
        3
    hughwu  
       2018-02-24 11:35:34 +08:00
    你好,我也遇到了这个编码问题,请问你怎么解决的?
    sherry00
        4
    sherry00  
    OP
       2018-07-13 17:34:04 +08:00
    @hughwu 'Content-Length': Buffer.byteLength(post_data, 'utf-8') //post_data.length 有中文不能用此方法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3294 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:37 · PVG 20:37 · LAX 05:37 · JFK 08:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.