Slack Webhook - Getting Invalid_Payload

邮差的信 提交于 2020-01-04 02:57:10

问题


I am attempting to set up a webhook to Slack, but am getting an Error message of "Invalid_Payload"

I've looked through Stack, Slack, and Github... but cant' find the answer I seek.

"CustomLink" in there for privacy, actual link is begin used.

CODE:

var request = require('request')

var webhook = "https://hooks.slack.com/services/CUSTOMLINK"

var payload={"text":"This is via an integration from Me - It is a test"}

request.post({url: webhook, payload: payload}, function(err, res){
    if(err){console.log(err)}
    if(res){console.log(res.body)}
})

ERROR:

 invalid_payload

回答1:


var payload= {"text":"This is via an integration from Me - It is a test"}
payload = JSON.stringify(payload)

I had forgot to stringify the JSON I was creating. Stupid Me.




回答2:


This worked for me

var payload = {"text":"Message to be sent"}
payload = JSON.stringify(payload);

request.post({url: url, body: payload},function(err,data){
    console.log(data.body);
})



回答3:


My guess is that you are missing the Content-type: application/json header. Then it doesn't recognize the json you are sending as json correctly.

You could try:

var request = require('request')

var webhook = "https://hooks.slack.com/services/CUSTOMLINK"

var payload={"text":"This is via an integration from Me - It is a test"}

var headers = {"Content-type": "application/json"}

request.post({url: webhook, payload: payload, headers: headers}, function(err, res){
    if(err){console.log(err)}
    if(res){console.log(res.body)}
})

Check the "Send it directly in JSON" here for reference




回答4:


var request = require('request');
var apiurl = webhookurl;

var payload= {
  username:'myusername',
  text:'test'
}
payload = JSON.stringify(payload);

request.post(
  {
  url:apiurl,
    form:payload
  }, function (err, result, body) {

    if(err) {
      return err;
    } else {

      console.log(body);
    }

  });


来源:https://stackoverflow.com/questions/39925395/slack-webhook-getting-invalid-payload

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