简体   繁体   English

HTTPS 节点 JS POST 问题

[英]HTTPS Node JS POST issue

I'm using https for this POST request.我正在为这个 POST 请求使用 https。

Could someone point out the issue here?有人可以在这里指出这个问题吗? I get undefined and no error message.我得到未定义且没有错误消息。

I tried wrapping the request before res.write with no success.我尝试在 res.write 之前包装请求,但没有成功。 I'm getting a我得到一个

statusCode: 400
Bad Payload received by generic incoming webhook.

Thanks!谢谢!

connector.js连接器.js

 const https = require('https'); var dataString = {"text": "Hello World"}; const deets = { portal_trade_events: { host: "outlook.office.com", path: "/path", headers: { 'Content-Type': 'application/json', 'Content-Length': dataString.text.length } } }; const options = { hostname: deets.portal_trade_events.host, port: 443, path: deets.portal_trade_events.path, method: 'POST', headers: deets.portal_trade_events.headers }; function post(options, data) { const req = https.request(options, (res) => { console.log(`statusCode: ${res.statusCode}`); res.on('data', (d) => { process.stdout.write(d) }); }); req.on('error', (error) => { console.error(error) }); req.write(JSON.stringify(data)); req.end() } post(options, dataString);

At the very least, your headers for content-length look wrong.至少,您的内容长度标题看起来是错误的。 You're sending the full JSON, not just the one text value.您发送的是完整的 JSON,而不仅仅是一个文本值。

const https = require('https');

const data = JSON.stringify({
  text: 'Hello World'
});

const options = {
  hostname: 'outlook.office.com',
  port: 443,
  path: '/path',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length
  }
};

const req = https.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`);
  res.on('data', (d) => {
    process.stdout.write(d);
  });
});
req.on('error', (error) => {
  console.error(error);
});
req.write(data);
req.end();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM