'Slack Webhook - Getting Invalid_Payload

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


Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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

  });

Solution 4:[4]

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"
}'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kaustav Ganguli
Solution 2 andresk
Solution 3 Alon Carmel
Solution 4 Tyler2P