Microsoft Bot Framework 请求中的 MissingProperty 错误

MissingProperty error in Microsoft Bot Framework Request

我正在开发一个使用 Microsoft Bot Framework 的应用程序。我的应用程序是用 Node 编写的。此时,我正在尝试使用以下代码 POST 一个 activity:

var https = require('https');

var token = '[receivedToken]';
var conversationId = '[conversationId]';

var options = {
  host: 'directline.botframework.com',
  port: 443,
  headers: {
    'Authorization': 'Bearer ' + token'
  },
  path: '/v3/directline/conversations/' + conversationId + '/activities',
  method: 'POST'                                
};

var request = https.request(options, (res) => {
  console.log(res.statusCode);
  var body = [];
  res.on('data', (d) => {
    body.push(d);
  });

  res.on('end', () => {
    var result = JSON.parse(Buffer.concat(body).toString());
    console.log(result);
  });
});

var info = { 
  type: 'message',
  text: 'test',
  from: { id: 'user_' + conversationId }
};

request.write(querystring.stringify(info));
request.end();

request.on('error', (err) => {
  console.log(err);
});

当此代码为 运行 时,我收到错误消息。这是状态代码 400 的错误,具有以下内容:

{ 
  error: { 
    code: 'MissingProperty',
    message: 'Invalid or missing activities in HTTP body' 
  }
}

我不明白 属性 缺少什么。一切看起来都正确。

您在请求 headers 中遗漏了 Content-TypeContent-Length

请考虑以下代码片段:

var https = require('https');

var token = '[receivedToken]';
var conversationId = '[conversationId]';

var info = JSON.stringify({
  type: 'message',
  text: 'test',
  from: { id: 'user_' + conversationId }
})

var options = {
  host: 'directline.botframework.com',
  port: 443,
  headers: {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(info)
  },
  path: '/v3/directline/conversations/' + conversationId + '/activities',
  method: 'POST'                                
};

var request = https.request(options, (res) => {
  console.log(res.statusCode);
  var body = [];
  res.on('data', (d) => {
    body.push(d);
  });

  res.on('end', () => {
    var result = JSON.parse(Buffer.concat(body).toString());
    console.log(result);
  });
});

request.write(info);
request.end();

request.on('error', (err) => {
  console.log(err);
});