1

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 Answer 1

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.