1

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

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

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

4

1 回答 1

1

您错过了请求标头中的Content-Type和。Content-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);
});
于 2017-01-19T05:12:59.153 回答