14

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
KJ Carlson
  • 215
  • 1
  • 2
  • 11

5 Answers5

7

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);
})
6
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.

KJ Carlson
  • 215
  • 1
  • 2
  • 11
  • It worked for me by setting the header: `{"Content-type": "application/x-www-form-urlencoded"` – mgalic Jul 12 '18 at 10:04
4

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

andresk
  • 2,845
  • 1
  • 13
  • 17
1
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);
    }

  });
Alon Carmel
  • 660
  • 1
  • 6
  • 16
1

Try with postman for sending the post request by using your webhook as URL and under body use raw and use { "text":"hello" } and follow the following image:

postman

or use this curl command:

curl --location --request POST 'https://hooks.slack.com/services/o1GLCDvsanqNDqMHCBQAd7F3' \
--header 'Content-Type: application/json' \
--data-raw '{
    "text": "hello"
}'
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Hritik
  • 11
  • 1