【问题标题】:Sending JSON data to a server with NODEJS HTTPS使用 NODEJS HTTPS 将 JSON 数据发送到服务器
【发布时间】:2021-10-15 13:30:47
【问题描述】:

我正在尝试向api.telegram.org 发出POST 请求,其中path/bot{token}/sendMessage。我用XMLHttpRequest客户端 完成了它,但是nodejs 不支持XMLHttpRequest。所以,替代方案是nodejs 中的https 模块。我看了一下this stackoverflow question,我的代码是:


var https = require("https");
var bot_token = "1677044698:AAE3GG-Tpfwgra41lenP5sw0HCIX-ZoTlLA";

var params = {
  "chat_id": "1105434113",
  "text": "The bot is working..."
}

var postData = JSON.stringify(params);

var options = {
  hostname: "api.telegram.org",
  path: `/bot${bot_token}/sendMessage`,
  port: 80,   
  method: 'POST',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  }
}

var req = https.request(
options,
  (res)=>{
    console.log(`STATUS: ${res.statusCode}`);
    console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
    res.setEncoding('utf8');
    res.on('data', (chunk) => {
      console.log(`BODY: ${chunk}`);
    });
    res.on('end', () => {
      console.log('No more data in response.');
    });
  }
)
req.on("error", (err)=>{
  console.log(err)
})


req.write(postData);
req.end();

但我遇到了一个我不知道的错误。

Error: write EPROTO 5912:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:332:

    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:94:16) {
  errno: -4046,
  code: 'EPROTO',
  syscall: 'write'
}
Do not worry about the exposed bot-token, I have revoked it.

还有,谢谢。

【问题讨论】:

    标签: javascript node.js post https telegram-api


    【解决方案1】:

    看起来像一个 ssl 错误。如果请求是通过 https 发出的,请将其更改为 http

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-27
    • 2012-04-15
    • 2011-07-28
    • 2013-11-22
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多