松弛事件请求URL-未验证的请求错误

时间:2018-12-01 22:48:08

标签: javascript json http slack slack-api

我的Slackbot需要通过Glitch应用程序向Microsoft Azure的Text Analytics API发出HTTP请求,但我收到以下错误消息:

An unverified request was sent to the Slack events Request URL. Request body: undefined

我怀疑该错误可能与Azure API调用无关,但发生在没有正确验证令牌的情况下向Glitch发出请求时,但是我检查了我的.env文件,并且令牌似乎在那里。

negativity: function(slack, message) {
var obj = { 
  documents: [{
  language: "en",
  id: 1,
  text: "hello it's a great day"
}]};

var JSONString = JSON.stringify(obj);

var request = new XMLHttpRequest();

request.onreadystatechange= function () {
if (request.readyState==4 && request.status == 200) { 

  var resultText = request.responseText;
     slack.chat.postMessage({
    channel: message.channel,
     text: `inside callback from negativity`
  })

  }
}

request.open("POST", "x");

request.setRequestHeader("Ocp-Apim-Subscription-Key", "x");
request.setRequestHeader("Content-Type","application/json");
request.setRequestHeader("Accept","application/json");
request.send(JSONString);
}

1 个答案:

答案 0 :(得分:0)

在获得一些帮助之后,看来var request = new XMLHttpRequest()在服务器端JavaScript上不可用,而可行的替代方法是https://www.npmjs.com/package/axios

此外,我还使用npm install --save axios在控制台中安装了axios。

我已经将上述代码附加了改进之处。

const axios = require('axios');

// some code

negativity: function(slack, message) {

        axios.post( 'x',
          {
            documents: [{
              language: "en",
              id: 1,
              text: message.text
            }]
          },
          {
            headers: {  
              'Ocp-Apim-Subscription-Key':'x',
              'Content-Type' : 'application/json',
              'Accept' :'application/json'  
            }
          }).then(function (response) {
              var output = response.data;
             });
          }