MissingProperty error in Microsoft Bot Framework Request

随声附和 提交于 2019-12-24 08:18:18

问题


I am working on an app that uses the Microsoft Bot Framework. My app is written in Node. At this time, I am trying to POST an activity using the following code:

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);
});

When this code is ran, I receive an error. It's an error of status code 400 which has the following:

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

I don't understand what property is missing though. Everything looks correct.


回答1:


You missed Content-Type and Content-Length in your request headers.

Please consider the following code snippet:

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);
});


来源:https://stackoverflow.com/questions/41727907/missingproperty-error-in-microsoft-bot-framework-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!