Search code examples
javascriptajaxhubotslack-api

calling slack-hubot api with javascript _ result body always undefined


but it always return undefined body.

Here is my source code. : static variables

const TOKEN = 'xoxp-7186818662-7186899793-7811139362-ccc6df';
const emojiAPIUrl = 'https://slack.com/api/emoji.list';

:uses

module.exports = function (robot) {
    robot.hear(/show emoji/igm, function(msg){
        var paramData = {'token' : TOKEN};
        var result = robot.http(emojiAPIUrl)
                        .headers({'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/x-www-form-urlencoded'})
                        .post(paramData, function(error, res, body){
                            console.log('err >> ' + error);
                            console.log('res >> ' + stringifyObject(res));
                            console.log('body >> ' + body);
                        });
    });
};

Following is result I got.

2015-07-19T06:22:27.303867+00:00 app[web.1]: err >> Error: socket hang up
2015-07-19T06:22:27.303960+00:00 app[web.1]: res >> 
2015-07-19T06:22:27.304014+00:00 app[web.1]: body >> undefined

You can check the result by calling below url

https://slack.com/api/emoji.list?token=xoxp-7186818662-7186899793-7811139362-ccc6df

I do not get why can not get json result like url. Thanks :D

P.S. If you know hubot scripting guide with javascript, please share it :D. Lots of samples on web, but mostly coffee script so hard to refer:-<


Solution

  • That in the URL https://slack.com/api/emoji.list?token=xoxp-7186818662-7186899793-7811139362-ccc6df isn't a POST it's a GET so you should do something like this:

    robot.http("https://slack.com/api/emoji.list?token=xoxp-7186818662-7186899793-7811139362-ccc6df")
      .get()(function(err, res, body) {
         console.log(body);
      }
    );