Invalid-channel error when using REST API chat.postMessage

#1

Hi

I’m getting an ‘invalid-channel’ error when using chat.postMessage rocket chat API

my code is shown below, can anyone see what i’m doing wrong here? thanks!

Its worth mentioning I’ve tried different formats for the channel name in the code (which incidentally is a public one)

  • “My_Chat_Room”
  • @My_Chat_Room
  • #My_Chat_Room
  • all the above in lowercase, with and without a space before the ‘My_Chat_Room’

function postMsg(){
var request = require(‘request’);

var url = ‘https://my.rocketchat.server/api/v1/login/’;
var user = ‘Test_User’;
var pass = ‘DFSdnwe89eq339’;

// Save these for future requests
var userId;
var authToken;

request.post(
{
uri: url,
form: { username: user, password: pass }
},
function(err, httpResponse, body) {
if (err) {
return console.error(‘Login failed:’, err);
}
var json = JSON.parse(body);
authToken = json.data.authToken;
userId = json.data.userId;
console.log(‘Login successful!:’ + “\n” + "User ID: " + userId + “\n” + "Auth Token: " + authToken);

  var postMsgUrl = 'https://my.rocketchat.server/api/v1/chat.postMessage';

  request.post({
  uri: postMsgUrl, 
  headers: {
  'X-User-Id': userId,
  'X-Auth-Token': authToken
  },
  data: {
  "channel": "My_Chat_Room", 
  "text": "test message", 
  }
  }, function(err, httpResponse, body) {
    if (err) {
      console.log('post failed:', err, body);
    }

console.log('Server responded with:', httpResponse, body);
});

}
)}