Microsoft Bot Framework Request中的MissingProperty错误

时间:2017-01-18 19:30:49

标签: javascript node.js azure botframework direct-line-botframework

我正在开发一个使用Microsoft Bot Framework的应用程序。我的应用程序是用Node编写的。目前,我正在尝试使用以下代码发布活动:

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' 
  }
}

我不明白遗失了什么财产。一切看起来都是正确的。

1 个答案:

答案 0 :(得分:1)

您在请求标头中错过了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);
});