我正在尝试为Slack设置一个webhook,但是收到错误消息" Invalid_Payload"
我已经浏览过Stack,Slack和Github ......但是不能这样做。找到我寻求的答案。
" CustomLink"在那里为了隐私,实际链接开始使用。
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)}
})
错误:
invalid_payload
答案 0 :(得分:3)
var payload= {"text":"This is via an integration from Me - It is a test"}
payload = JSON.stringify(payload)
我忘了将我正在创建的JSON字符串化。愚蠢的我。
答案 1 :(得分:2)
我的猜测是你错过了Content-type: application/json
标题。然后它无法正确识别您作为json发送的json。
你可以尝试:
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)}
})
选中“以JSON直接发送”here以供参考
答案 2 :(得分:2)
这对我有用
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 :(得分: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);
}
});