sherry00
V2EX  ›  问与答

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

  •  
  •   sherry00 · Apr 13, 2017 · 6206 views
    This topic created in 3344 days ago, the information mentioned may be changed or developed.
    大致代码:
    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 replies    2018-07-13 17:34:04 +08:00
    sherry00
        1
    sherry00  
    OP
       Apr 13, 2017
    不知道有什么好的办法能查明这个问题的原因
    sherry00
        2
    sherry00  
    OP
       Apr 13, 2017
    我来结贴了。
    原来是中文编码的问题引起的。
    hughwu
        3
    hughwu  
       Feb 24, 2018
    你好,我也遇到了这个编码问题,请问你怎么解决的?
    sherry00
        4
    sherry00  
    OP
       Jul 13, 2018
    @hughwu 'Content-Length': Buffer.byteLength(post_data, 'utf-8') //post_data.length 有中文不能用此方法
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5254 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 05:54 · PVG 13:54 · LAX 22:54 · JFK 01:54
    ♥ Do have faith in what you're doing.